def create(project: Project, user: User) -> Job: """ Snapshot the project. """ snapshot = Snapshot.objects.create(project=project, creator=user) subjobs = [] # Clean the project's working directory subjobs.append(project.cleanup(user)) # Pull the project's sources subjobs.append(project.pull(user)) # "Reflow" the project (regenerate derived files) reflow = project.reflow(user) if reflow: subjobs.append(reflow) # Pin the container image subjobs.append( project.pin(user, **Job.create_callback(snapshot, "pin_callback"))) # Create an index.html if a "main" file is defined main = project.get_main() if main: options = {} theme = project.get_theme() if theme: options["theme"] = theme subjobs.append(main.convert(user, "index.html", options=options)) # This is currently required to populate field `zip_name` below snapshot.save() # Archive the working directory to the snapshot directory subjobs.append( project.archive( user, snapshot=snapshot.id, path=f"{project.id}/{snapshot.id}/{snapshot.zip_name}", **Job.create_callback(snapshot, "archive_callback"), )) job = Job.objects.create( method=JobMethod.series.name, description="Snapshot project '{0}'".format(project.name), project=project, creator=user, ) job.children.set(subjobs) job.dispatch() snapshot.job = job snapshot.save() return snapshot
def convert(self, user: User, output: str, options: Dict = {}, snapshot: bool = False) -> Job: """ Convert a file to another format. Creates a `convert` job which returns a list of files produced (may be more than one e.g a file with a media folder). Each of the files will have this file as an upstream dependency. For certain target formats (e.g. `gdoc`), a source is also created (e.g. `GoogleDocsSource`) in the job callback. The source will have this new file as a downstream dependant, and this file will have the new file as an upstream dependency. Do not call back if this conversion is for a snapshot (do not want a file entry for those at present). """ if self.mimetype: options["from"] = self.mimetype return Job.objects.create( project=self.project, creator=user, description="Convert '{0}' to '{1}'".format(self.path, output), method=JobMethod.convert.name, params=dict(input=self.path, output=output, options=options), secrets=GoogleSourceMixin().get_secrets(user) if output.endswith(".gdoc") else None, **(Job.create_callback(self, "convert_callback") if not snapshot else {}))
def extract(self, review, user: Optional[User] = None, filters: Optional[Dict] = None) -> Job: """ Extract a review from a project source. Creates a job, and adds it to the source's `jobs` list. Note: the jobs callback is `Review.extract_callback`. """ source = self.to_address() source["type"] = source.type_name description = "Extract review from {0}" description = description.format(self.address) job = Job.objects.create( project=self.project, creator=user or self.creator, method=JobMethod.extract.value, params=dict(source=source, filters=filters), description=description, secrets=self.get_secrets(user), **Job.create_callback(review, "extract_callback"), ) self.jobs.add(job) return job
def pull(self, user: Optional[User] = None) -> Job: """ Pull the source to the filesystem. Creates a job, and adds it to the source's `jobs` list. """ source = self.to_address() source["type"] = source.type_name description = "Pull {0}" if self.type_class == "UploadSource": description = "Collect {0}" description = description.format(self.address) job = Job.objects.create( project=self.project, creator=user or self.creator, method=JobMethod.pull.value, params=dict(source=source, path=self.path), description=description, secrets=self.get_secrets(user), **Job.create_callback(self, "pull_callback"), ) self.jobs.add(job) return job
def register(self, user: Optional[User] = None) -> Job: """ Register the DOI. Creates a job that asynchronously registers the DOI with a registration agency. """ job = Job.objects.create( description="Register DOI", method=JobMethod.register.name, params=dict( node=self.node.json, doi=self.doi, url=self.url, batch=f"{self.id}@{time.time()}", ), project=self.node and self.node.project, creator=user, **Job.create_callback(self, "register_callback"), ) self.job = job self.save() return job