Tweaks to baker to use the final directory structure
This commit is contained in:
@@ -59,7 +59,7 @@ $conduit = id(new ConduitClient('https://developer.blender.org/api/'))
|
||||
->setConduitToken(trim(file_get_contents($CONDUIT_TOKEN_FILE)));
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// Baking logic and loop.
|
||||
// Baking utilities.
|
||||
|
||||
function EnsureDirectoryOrDie($dir) {
|
||||
if (!is_dir($dir)) {
|
||||
@@ -70,11 +70,79 @@ function EnsureDirectoryOrDie($dir) {
|
||||
}
|
||||
}
|
||||
|
||||
function EnsureOutputDirectoryOrDie() {
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// Revision baking.
|
||||
|
||||
class RevisionInfo {
|
||||
public $title = '';
|
||||
public $page_title = '';
|
||||
|
||||
public $last_diff_id = 0;
|
||||
|
||||
public $hash = '';
|
||||
|
||||
public static function ReadFromFile($file_name) {
|
||||
$info = new RevisionInfo();
|
||||
|
||||
if (!file_exists($file_name)) {
|
||||
return $info;
|
||||
}
|
||||
|
||||
$json = json_decode(file_get_contents($file_name));
|
||||
foreach ($json as $key => $value) {
|
||||
$info->{$key} = $value;
|
||||
}
|
||||
|
||||
return $info;
|
||||
}
|
||||
|
||||
public function SaveToFile($file_name) {
|
||||
$json_str = json_encode(get_object_vars($this));
|
||||
file_put_contents($file_name, $json_str);
|
||||
}
|
||||
};
|
||||
|
||||
function GetRevisionOutputDirectory($revision) {
|
||||
global $OUTPUT_DIR;
|
||||
EnsureDirectoryOrDie($OUTPUT_DIR);
|
||||
|
||||
$id = $revision->getID();
|
||||
|
||||
return PathJoin(array($OUTPUT_DIR, 'differential', SubpathFromId($id)));
|
||||
}
|
||||
|
||||
function EnsureRevisionOutputDirectory($revision) {
|
||||
$dir = GetRevisionOutputDirectory($revision);
|
||||
|
||||
EnsureDirectoryOrDie($dir);
|
||||
EnsureDirectoryOrDie(PathJoin(array($dir, 'raw_diff')));
|
||||
|
||||
return $dir;
|
||||
}
|
||||
|
||||
function GetRevisionInfoFilename($revision) {
|
||||
$revision_dir = GetRevisionOutputDirectory($revision);
|
||||
return PathJoin(array($revision_dir, 'info.json'));
|
||||
}
|
||||
|
||||
function GetRevisionHTMLFilename($revision, $diff_id) {
|
||||
$revision_id = $revision->getID();
|
||||
$revision_dir = GetRevisionOutputDirectory($revision);
|
||||
|
||||
$file_name = "D{$revision_id}.id{$diff_id}.html";
|
||||
|
||||
return PathJoin(array($revision_dir, $file_name));
|
||||
}
|
||||
|
||||
function GetRevisionRawDiffFilename($revision, $diff_id) {
|
||||
$revision_id = $revision->getID();
|
||||
$revision_dir = GetRevisionOutputDirectory($revision);
|
||||
|
||||
$file_name = "D{$revision_id}.id{$diff_id}.diff";
|
||||
|
||||
return PathJoin(array($revision_dir, 'raw_diff', $file_name));
|
||||
}
|
||||
|
||||
/*
|
||||
function MakeGenericOutputDirectory($subject) {
|
||||
global $OUTPUT_DIR;
|
||||
|
||||
@@ -105,27 +173,9 @@ function MakeRawDiffOutputFileName($revision, $diff_id) {
|
||||
return MakeRevisionBaseOutputFileName(
|
||||
$revision, $diff_id, 'raw_diff') . '.diff';
|
||||
}
|
||||
*/
|
||||
|
||||
function MakePasteOutputFileNameFromHandle($handle) {
|
||||
return MakeGenericOutputDirectory('paste') .
|
||||
DIRECTORY_SEPARATOR .
|
||||
$handle->getName() . '.txt';
|
||||
}
|
||||
|
||||
|
||||
function MakeFileOutputFileNameFromFile($file) {
|
||||
$dir = MakeGenericOutputDirectory('file');
|
||||
|
||||
$id = $file->getID();
|
||||
|
||||
EnsureDirectoryOrDie($dir . DIRECTORY_SEPARATOR . 'F' . $id);
|
||||
|
||||
return $dir.
|
||||
DIRECTORY_SEPARATOR .
|
||||
$file->getSafeFileNameForBake();
|
||||
}
|
||||
|
||||
function BakeDifferentialToResponse($revision, $diff_id = NULL) {
|
||||
function RenderDifferentialToResponse($revision, $diff_id) {
|
||||
global $application_configuration;
|
||||
global $revision_controller;
|
||||
global $routing_map;
|
||||
@@ -133,12 +183,6 @@ function BakeDifferentialToResponse($revision, $diff_id = NULL) {
|
||||
|
||||
$revision_id = $revision->getID();
|
||||
|
||||
if (is_null($diff_id)) {
|
||||
printf('Baking D' . $revision_id . ' ...' . "\n");
|
||||
} else {
|
||||
printf('Baking D' . $revision_id . ', diff ' . $diff_id . ' ...' . "\n");
|
||||
}
|
||||
|
||||
$path = '/D' . $revision_id;
|
||||
|
||||
$route_result = $routing_map->routePath($path);
|
||||
@@ -165,7 +209,7 @@ function BakeDifferentialToResponse($revision, $diff_id = NULL) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
function BakeResponseToHTML($response) {
|
||||
function RenderResponseToHTML($response) {
|
||||
$html = '';
|
||||
foreach ($response->renderChildren() as $child) {
|
||||
$html .= $child->render();
|
||||
@@ -174,71 +218,6 @@ function BakeResponseToHTML($response) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
function StorePasteHandle($handle) {
|
||||
global $viewer;
|
||||
|
||||
$file_name = MakePasteOutputFileNameFromHandle($handle);
|
||||
if (file_exists($file_name)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$paste = id(new PhabricatorPasteQuery())
|
||||
->setViewer($viewer)
|
||||
->withPHIDs(array($handle->getPHID()))
|
||||
->needRawContent(true)
|
||||
->executeOne();
|
||||
|
||||
file_put_contents($file_name, $paste->getRawContent());
|
||||
}
|
||||
|
||||
function StoreFile($file) {
|
||||
print("Loading and storing file F{$file->getID()}\n");
|
||||
$file_name = MakeFileOutputFileNameFromFile($file);
|
||||
file_put_contents($file_name, $file->loadFileData());
|
||||
}
|
||||
|
||||
function StoreFileHandle($handle) {
|
||||
global $viewer;
|
||||
|
||||
$file = id(new PhabricatorFileQuery())
|
||||
->setViewer($viewer)
|
||||
->withPHIDs(array($handle->getPHID()))
|
||||
->needTransforms(
|
||||
array(
|
||||
PhabricatorFileThumbnailTransform::TRANSFORM_PREVIEW,
|
||||
))
|
||||
->executeOne();
|
||||
|
||||
$file_name = MakeFileOutputFileNameFromFile($file);
|
||||
if (file_exists($file_name)) {
|
||||
return;
|
||||
}
|
||||
|
||||
StoreFile($file);
|
||||
|
||||
$preview_xform = $file->getTransform(PhabricatorFileThumbnailTransform::TRANSFORM_PREVIEW);
|
||||
if ($preview_xform) {
|
||||
StoreFile($preview_xform);
|
||||
}
|
||||
}
|
||||
|
||||
function StoreResponseAttachments($response) {
|
||||
$user = $response->getRequest()->getUser();
|
||||
$handlePool = $user->getHandlePool();
|
||||
$handles = $handlePool->getHandles();
|
||||
|
||||
foreach ($handles as $phid => $handle) {
|
||||
$type = $handle->getType();
|
||||
if ($type == PhabricatorPastePastePHIDType::TYPECONST) {
|
||||
StorePasteHandle($handle);
|
||||
} else if ($type == PhabricatorFileFilePHIDType::TYPECONST) {
|
||||
StoreFileHandle($handle);
|
||||
} else {
|
||||
// print("$type\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function StoreRawDiff($revision, $diff_id) {
|
||||
if (is_null($diff_id)) {
|
||||
return;
|
||||
@@ -247,13 +226,6 @@ function StoreRawDiff($revision, $diff_id) {
|
||||
global $viewer;
|
||||
global $conduit;
|
||||
|
||||
$output_file = MakeRawDiffOutputFileName($revision, $diff_id);
|
||||
if (file_exists($output_file)) {
|
||||
return;
|
||||
}
|
||||
|
||||
print("Requesting and storing the raw diff ...\n");
|
||||
|
||||
$diff = id(new DifferentialDiffQuery())
|
||||
->setViewer($viewer)
|
||||
->withIDs(array($diff_id))
|
||||
@@ -271,24 +243,84 @@ function StoreRawDiff($revision, $diff_id) {
|
||||
$bundle->setConduit($conduit);
|
||||
$raw_diff = $bundle->toGitPatch();
|
||||
|
||||
$output_file = GetRevisionRawDiffFilename($revision, $diff_id);
|
||||
file_put_contents($output_file, $raw_diff);
|
||||
}
|
||||
|
||||
function BakeDifferentialToFile($revision, $diff_id = NULL) {
|
||||
$response = BakeDifferentialToResponse($revision, $diff_id);
|
||||
function GetRevisionHash($revision) {
|
||||
global $viewer;
|
||||
|
||||
$html = BakeResponseToHTML($response);
|
||||
$xaction = id(new DifferentialTransactionQuery())
|
||||
->setViewer($viewer)
|
||||
->withObjectPHIDs(array($revision->getPHID()))
|
||||
->setOrder('newest')
|
||||
->setLimit(1)
|
||||
->executeOne();
|
||||
|
||||
$output_file = MakeRevisionHTMLOutputFileName($revision, $diff_id);
|
||||
file_put_contents($output_file, $html);
|
||||
$digest = '';
|
||||
|
||||
file_put_contents($output_file . '.title', $response->getTitle());
|
||||
$digest .= $revision->getDateModified();
|
||||
$digest .= '_' . $xaction->getPHID();
|
||||
$digest .= '_' . $xaction->getDateModified();
|
||||
|
||||
StoreResponseAttachments($response);
|
||||
StoreRawDiff($revision, $diff_id);
|
||||
return $digest;
|
||||
}
|
||||
|
||||
EnsureOutputDirectoryOrDie();
|
||||
function NeedBakeRevision($info, $revision, $diff_id) {
|
||||
if (!file_exists(GetRevisionHTMLFilename($revision, $diff_id))) {
|
||||
return true;
|
||||
}
|
||||
if (!file_exists(GetRevisionRawDiffFilename($revision, $diff_id))) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($info->last_diff_id < $diff_id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($revision->getTitle() != $info->title) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (GetRevisionHash($revision) != $info->hash) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function BakeRevision($revision, $diff_id) {
|
||||
$revision_id = $revision->getID();
|
||||
|
||||
printf('Baking D' . $revision_id . '?id=' . $diff_id . ' ...' . "\n");
|
||||
|
||||
$info_file_name = GetRevisionInfoFilename($revision);
|
||||
|
||||
$info = RevisionInfo::ReadFromFile($info_file_name);
|
||||
|
||||
if (!NeedBakeRevision($info, $revision, $diff_id)) {
|
||||
print(' ... ignoring: up to date' . "\n");
|
||||
return;
|
||||
}
|
||||
|
||||
$response = RenderDifferentialToResponse($revision, $diff_id);
|
||||
|
||||
$html = RenderResponseToHTML($response);
|
||||
$html_file_name = GetRevisionHTMLFIlename($revision, $diff_id);
|
||||
file_put_contents($html_file_name, $html);
|
||||
|
||||
StoreRawDiff($revision, $diff_id);
|
||||
|
||||
$info->title = $revision->getTitle();
|
||||
$info->page_title = $response->getTitle();
|
||||
$info->hash = GetRevisionHash($revision);
|
||||
$info->last_diff_id = max($info->last_diff_id, $diff_id);
|
||||
|
||||
$info->SaveToFile($info_file_name);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// Baking main loop.
|
||||
|
||||
print('Querying differential revisions from the database ... ' . "\n");
|
||||
$revisions = id(new DifferentialRevisionQuery())
|
||||
@@ -298,10 +330,10 @@ $revisions = id(new DifferentialRevisionQuery())
|
||||
->execute();
|
||||
|
||||
foreach ($revisions as $revision_id => $revision) {
|
||||
BakeDifferentialToFile($revision);
|
||||
EnsureRevisionOutputDirectory($revision);
|
||||
|
||||
foreach ($revision->getDiffIDs() as $diff_id) {
|
||||
BakeDifferentialToFile($revision, $diff_id);
|
||||
BakeRevision($revision, $diff_id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user