def create_snapshot( userId, projectId ): ex = os.path.join( g_snapdir, g_snapexec ) print ex schpicid = uuid.uuid4() brdpicid = uuid.uuid4() #schfn = FILEPATH + "/" + str(schpicid) #brdfn = FILEPATH + "/" + str(brdpicid) sp.call( [ "mkdir", "-p", os.path.join( USRBASE, userId, projectId, "img" ) ] ) schfn = os.path.join( USRBASE, userId, projectId, "img", str(schpicid) ) brdfn = os.path.join( USRBASE, userId, projectId, "img", str(brdpicid) ) ee = [ "node", ex, "-p", projectId, "-u", userId, "-s", schfn, "-b", brdfn ] print ee sp.call( ee ) st = mew.secondTime() ht = mew.humanTime() projpic = {} projpic["userId"] = userId projpic["projectId"] = projectId projpic["schPicId"] = str(schpicid) projpic["brdPicId"] = str(brdpicid) projpic["stime"] = st projpic["timestamp"] = ht db.hmset( "projectpic:" + str(projectId), projpic ) proj = mew.getProject( projectId ) pic = {} pic["id"] = str(schpicid) pic["permission"] = proj["permission"] pic["userId"] = userId pic["clientToken"] = "" pic["projectId"] = projectId pic["stime"] = st pic["timestamp"] = ht db.hmset( "pic:" + str(schpicid) , pic ) pic = {} pic["id"] = str(brdpicid) pic["permission"] = proj["permission"] pic["userId"] = userId pic["clientToken"] = "" pic["projectId"] = projectId pic["stime"] = st pic["timestamp"] = ht db.hmset( "pic:" + str(brdpicid) , pic )
sp.check_call( [ "node", exec_snap, "-i", loc, "-W", "200", "-H", "200", "-u", userId, "-o", os.path.join( base_fqfn, png_fn) ], env=myenv ) except Exception,ee: print mew.humanTime(), "libmod_snap_d error:", str(ee) return None pass # for open ... pass fp.close() print mew.humanTime(), fqfn_queue_file, os.path.join( LIBMOD_SNAP_QUEUE_DIR, ".store", qfn ) sp.check_call( [ "mkdir", "-p", os.path.join( LIBMOD_SNAP_QUEUE_DIR, ".store" ) ] ) os.rename( fqfn_queue_file, os.path.join( LIBMOD_SNAP_QUEUE_DIR, ".store", qfn ) ) # for listdir pass while True: print mew.humanTime(), ">" process_imports() time.sleep(sleepy)
def process_imports(): qfns = [] for f in os.listdir( LIBMOD_SNAP_QUEUE_DIR ): if os.path.isfile( os.path.join( LIBMOD_SNAP_QUEUE_DIR, f ) ) and f.endswith(".q"): qfns.append( f ) #qfns = [ f for f in os.listdir( LIBMOD_SNAP_QUEUE_DIR ) if os.path.isfile( os.path.join( LIBMOD_SNAP_QUEUE_DIR, f ) ) ] for qfn in qfns: if verbose: print mew.humanTime(), "libmod_snap_d>> processing", qfn fqfn_queue_file = os.path.join( LIBMOD_SNAP_QUEUE_DIR, qfn ) if verbose: print mew.humanTime(), "libmod_snap_d>> processing", fqfn_queue_file fp = open( fqfn_queue_file, "r" ) for line in fp: if verbose: print mew.humanTime(), "libmod_snap_d>>>", line if line[0] == '#': continue line = line.rstrip() fileId, fn, userId, portfolioId = line.split(',') try: if len(fn)==0: continue if verbose: print mew.humanTime(), "libmod_snap_d>>>>", fn base_fqfn = os.path.join( ODIR, "usr", userId ) if len(portfolioId) > 0: base_fqfn = os.path.join( ODIR, "usr", userId, portfolioId ) fqfn = os.path.join( base_fqfn, "json", ".store", fn ) if (fqfn.endswith("_component_list.json") or fqfn.endswith("_footprint_list.json")) and os.path.isfile( fqfn ): f = open(fqfn) list_json = json.load(f) f.close() for libname in list_json: for v in list_json[libname]["list"]: loc = v["data"] png_fn = re.sub( "\.json$", ".png", loc ) png_fn = re.sub( "/json/", "/png/", png_fn ) if verbose: print mew.humanTime(), "libmod_snap_d>>>> snap:", libname, loc, png_fn exec_snap = "" if fqfn.endswith("_component_list.json"): exec_snap = "../js/libsnap.js" elif fqfn.endswith("_footprint_list.json"): exec_snap = "../js/modsnap.js" myenv = os.environ.copy() myenv["NODE_PATH"] = "../js" sp.check_call( [ "mkdir", "-p", os.path.join( base_fqfn, os.path.dirname( png_fn ) ) ] ) sp.check_call( [ "node", exec_snap, "-i", loc, "-W", "200", "-H", "200", "-u", userId, "-o", os.path.join( base_fqfn, png_fn) ], env=myenv ) except Exception,ee: print mew.humanTime(), "libmod_snap_d error:", str(ee) return None pass # for open ... pass fp.close() print mew.humanTime(), fqfn_queue_file, os.path.join( LIBMOD_SNAP_QUEUE_DIR, ".store", qfn ) sp.check_call( [ "mkdir", "-p", os.path.join( LIBMOD_SNAP_QUEUE_DIR, ".store" ) ] ) os.rename( fqfn_queue_file, os.path.join( LIBMOD_SNAP_QUEUE_DIR, ".store", qfn ) )
def process_imports(): db = redis.Redis() qid = db.lpop("importq") while qid is not None: h = db.hgetall("importq:" + qid) userId = h["userId"] fileId = h["fileUUID"] fileName = h["fileName"] if h["type"] == "portfolio": try: if verbose: print mew.humanTime( ), "libmod_import_d>> processing", fileId, userId, fileName # Convert from KiCAD to MeowCAD json # lmi_out = sp.check_output([ "./libmod_import_file.py", "-i", ODIR + "/stage/" + fileId, "-o", ODIR + "/usr/" + userId, "--name", fileName ]) # Create the location and list files # lmll_out = sp.check_output([ "./libmodloclist.py", "-u", userId, "-D", ODIR + "/usr/" + userId + "/json/" ]) processed_loclist_fn = lmi_out.split("\n") tmpid = str(uuid.uuid4()) # Create the queue file # tmpfn = os.path.join("/tmp", tmpid) tmpfp = open(tmpfn, "w") for fn in processed_loclist_fn: if len(fn) == 0: continue fqfn = ODIR + "/usr/" + userId + "/json/.store/" + fn tmpfp.write(",".join([fileId, fqfn, userId, ""]) + "\n") tmpfp.close() # And place it so the snap daemon can process it # os.rename(tmpfn, os.path.join(LIBMOD_SNAP_QUEUE_DIR, fileId + ".q")) # Get the next queue id from the database # qid = db.lpop("importq") except Exception, ee: print mew.humanTime(), "libmodimport_d error:", str(ee) return None