def ro_root_directory(cmdname, ro_config, rodir): """ Find research object root directory Returns directory path string, or None if not found, in which case an error message is displayed. """ # log.debug("ro_root_directory: cmdname %s, rodir %s"%(cmdname, rodir)) # log.debug(" ro_config %s"%(repr(ro_config))) ro_dir = ro_utils.ropath(ro_config, rodir) if not ro_dir: print ("%s: indicated directory not in configured research object directory tree: %s (%s)" % (cmdname, rodir, ro_config['robase'])) return None if not os.path.isdir(ro_dir): print ("%s: indicated directory does not exist: %s" % (cmdname, rodir)) return None manifestdir = None ro_dir_next = ro_dir ro_dir_prev = "" # log.debug("ro_dir_next %s, ro_dir_prev %s"%(ro_dir_next, ro_dir_prev)) while ro_dir_next and ro_dir_next != ro_dir_prev: # log.debug("ro_dir_next %s, ro_dir_prev %s"%(ro_dir_next, ro_dir_prev)) manifestdir = os.path.join(ro_dir_next, ro_settings.MANIFEST_DIR) if os.path.isdir(manifestdir): return ro_dir_next ro_dir_prev = ro_dir_next ro_dir_next = os.path.dirname(ro_dir_next) # Up one directory level print ("%s: indicated directory is not contained in a research object: %s" % (cmdname, ro_dir)) return None
def create(progname, configbase, options, args): """ Create a new Research Object. ro create RO-name [ -d dir ] [ -i RO-ident ] """ ro_options = { "roname": getoptionvalue(args[2], "Name of new research object: "), "rodir": options.rodir or "", "roident": options.roident or "" } log.debug("cwd: " + os.getcwd()) log.debug("ro_options: " + repr(ro_options)) ro_options['roident'] = ro_options['roident'] or ro_utils.ronametoident(ro_options['roname']) # Read local ro configuration and extract creator ro_config = ro_utils.readconfig(configbase) timestamp = datetime.datetime.now().replace(microsecond=0) ro_options['rocreator'] = ro_config['username'] ro_options['rocreated'] = timestamp.isoformat() ro_dir = ro_utils.ropath(ro_config, ro_options['rodir']) if not ro_dir: print ("%s: research object not in configured research object directory tree: %s" % (ro_utils.progname(args), ro_options['rodir'])) return 1 # Create directory for manifest if options.verbose: print "ro create \"%(roname)s\" -d \"%(rodir)s\" -i \"%(roident)s\"" % ro_options manifestdir = os.path.join(ro_dir, ro_settings.MANIFEST_DIR) log.debug("manifestdir: " + manifestdir) try: os.makedirs(manifestdir) except OSError: if os.path.isdir(manifestdir): # Someone else created it... # See http://stackoverflow.com/questions/273192/ # python-best-way-to-create-directory-if-it-doesnt-exist-for-file-write pass else: # There was an error on creation, so make sure we know about it raise # Create manifest file # @@TODO: create in-memory graph and serialize that manifestfilename = os.path.join(manifestdir, ro_settings.MANIFEST_FILE) log.debug("manifestfilename: " + manifestfilename) manifest = ( """<?xml version="1.0" encoding="utf-8"?> <rdf:RDF xml:base=".." xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:ro="http://purl.org/wf4ever/ro#" xmlns:ore="http://www.openarchives.org/ore/terms/" xmlns:ao="http://purl.org/ao/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:foaf="http://xmlns.com/foaf/0.1/" > <ro:ResearchObject rdf:about=""> <dcterms:identifier>%(roident)s</dcterms:identifier> <dcterms:title>%(roname)s</dcterms:title> <dcterms:description>%(roname)s</dcterms:description> <dcterms:creator>%(rocreator)s</dcterms:creator> <dcterms:created>%(rocreated)s</dcterms:created> <!-- self-reference to include above details as annotation --> <ore:aggregates> <ro:AggregatedAnnotation> <ro:annotatesAggregatedResource rdf:resource="" /> <ao:body rdf:resource=".ro/manifest.rdf" /> </ro:AggregatedAnnotation> </ore:aggregates> </ro:ResearchObject> </rdf:RDF> """ % ro_options) log.debug("manifest: " + manifest) manifestfile = open(manifestfilename, 'w') manifestfile.write(manifest) manifestfile.close() return 0