예제 #1
0
def untar(tar_fp, dst=".", ext=".tar", mode="r", s=False, lc=False):
    tar_fp = expand(tar_fp)
    if not exists(tar_fp):
        if (s or STRICT):
            raise Exception, "File [%s] not found"
        else:
            perror("File [%s] not found" % tar_fp)
            err("File [%s] not found" % tar_fp)
        return

    if not tar_fp.endswith(ext): # try to guess the extension and mode
        for (ext_alt, mode_alt) in [(".tgz", "r:gz"), (".tar.gz", "r:gz"), (".tar.bz2", "r:bz2")]:
            if tar_fp.endswith(ext_alt):
                debug("File [%s] has extension [%s]" % (tar_fp, ext_alt))
                ext  = ext_alt
                mode = mode_alt

    try:
        pdebug("attempt untar: [%s] mode: [%s]" % (tar_fp, mode))
        tar_obj = tarfile.open(tar_fp, mode)
        pdebug("dst: [%s]" % dst)
        for m in tar_obj.getmembers():
            tar_obj.extract(m,dst)
        #tar_obj.extractall(dst) # extractall is from Python 2.5 onwards
        tar_obj.close()
    except Exception, e:
        if (s or STRICT):
            raise e
        else:
            perror("Cannot untar file [%s] in mode [%s]" % (tar_fp,mode))
            err("Cannot untar file [%s] in mode [%s]" % (tar_fp,mode))
        return
예제 #2
0
def clean_flist(raw_flist,s=False,lc=False):
    if not raw_flist:
        if (s or STRICT):
            raise Exception, "No file argument provided."
        else:
            perror("No file argument provided.")
            err("No file argument provided.")

    if type(raw_flist) != type(list()):
        raw_flist = [raw_flist]

    full_flist = []
    clean_list = []

    for f in raw_flist:
        full_flist.extend(glob(expand(f)))

    for entry in full_flist:
        valid = validate_path(entry)
        if exists(entry) and valid:
            clean_list.append(entry)
        elif not valid:
            warning("Invalid path: [%s] - skipping" % entry)
        else:
            warning("[%s] does not exist - skipping" % entry)

    if not clean_list:
        if (s or STRICT):
            raise Exception,"No such file or directory: %s" % raw_flist 
        else:
            warn("No such file or directory: [%s]" % raw_flist)

    return clean_list
예제 #3
0
def current_ip_address(ip_addr_service):
    request = None

    # Attempt to make an HTTP(S) request to the service that returns the
    # public IP of the host.
    request = requests.get(ip_addr_service)

    # Handle catastrophic failure.
    if not request:
        logging.err("Failed to contact HTTP(S) service " +
                    str(ip_addr_service) + " to get host's IP address.")
        return None

    # Handle HTTP error codes.
    if request.status_code != requests.codes.ok:
        logging.err("HTTP(S) request to IP address service " +
                    str(ip_addr_service) +
                    "failed, returned HTTP error code " +
                    str(request.status_code) + ".")
        return None

    # Got the host's public IP address.  Explicitly cast to a string to make
    # life easier four other modules.
    logging.debug("Got current IP address of host: " + str(request.text))
    return str(request.text)
예제 #4
0
def sysload(openwrt_host):
    logging.debug("Entered function openwrt.sysload().")

    request = None
    system_info = {}
    sysload = {}

    #  Contact the OpenWRT host and get system info.
    request = requests.get(openwrt_host + "/cgi-bin/system/info")
    if not request:
        logging.err("Failed to contact endpoint " + openwrt_host +
                    "/cgi-bin/system/info")
        sysload["one_minute"] = -1.0
        sysload["five_minute"] = -1.0
        sysload["fifteen_minute"] = -1.0
        return sysload

    # Extract the system load stats.
    system_info = json.loads(request.text)
    system_info = system_info["load"]

    # Due to the fact that OpenWRT does this strangely, we have to do some
    # math to get actual system load stats.  At least the 1, 5, and 15 minute
    # averages have stable positions.
    sysload["one_minute"] = round((system_info[0] / 65536.0), 2)
    sysload["five_minute"] = round((system_info[1] / 65536.0), 2)
    sysload["fifteen_minute"] = round((system_info[2] / 65536.0), 2)
    return sysload
예제 #5
0
def popd(s=False,lc=False):
    """
    Unix popd. Returns to the directory on top of dirstack, If dirstack is empty, it 
    does nothing.

    Pre-conditions: none
    Post-conditions: if dirstack is not empty, top directory is popped and becomes working
    directory. Otherwise, nothing changes.
    Return value: none

    Examples:
    popd()

    Possible errors: none
    """
    if len(dirstack) >= 1:
        info("%s <- %s" % (dirstack[-1], basename(pwd())))
        dp = dirstack.pop()
        try:
            chdir(dp)
        except Exception, e:
            if (s or STRICT):
                raise e
            else:
                perror("Cannot popd [%s]." % dp)
                err("Cannot popd [%s]." % dp)
예제 #6
0
    def get_most_profitable(self, timestamp, inflation=None):
        """Return what would have been the most profitable stock now 
		if you had bought before
		:param timestamp: date bought the stock 
		:param inflation: inflation rate between now and the given timestamp
		"""
        most_profitable = []
        timestamp = parse(timestamp)
        if not inflation: inflation = 1
        logging.info('Start computing most profitable stocks')
        try:
            for symbol, values in self.prices_columnwise.items():
                idx = values['timestamp'].index(timestamp)
                percentage = (values['close'][-1] - values['close'][idx] *
                              inflation) / (values['close'][idx] * inflation)
                most_profitable.append([symbol, percentage * 100])
            most_profitable = sorted(most_profitable,
                                     key=lambda x: x[1],
                                     reverse=True)
        except Exception as e:
            logging.err(
                "Error occurred during getting would have been the most profitable stocks",
                exc_info=True)
        logging.info('Done computing most profitable stocks')
        return most_profitable
예제 #7
0
파일: bot.py 프로젝트: nickthename/pibot
 def main(self):
     req = self.conf.check([
         "bot.nick",
         "bot.realname",
         "bot.server",
         "bot.port"
         ])
     if len(req) > 1:
         l.err("Cannot start bot. Missing required options:")
         for r in req:
             l.err("\t", r)
         sys.exit(1)
     self.plugin_manager.load("modules/")
     pldir = self.conf.get_value("bot.plugindir")
     if pldir is not None:
         self.plugin_manager.load(pldir)
     self.plugin_manager.initialize()
     l.info("Connecting...")
     self.sock.connect((self.conf.get_value("bot.server"),
         int(self.conf.get_value("bot.port"))))
     self.poller.add_read(self.sock, self.read_socket)
     self.poller.add_write(self.sock, self.first_write)
     self.poller.add_except(self.sock, self.disconnect)
     try:
         self.poller.mainloop()
     except KeyboardInterrupt:
         self.quit()
         self.dump_queue()
         self.terminate()
     except Exception as e:
         import traceback
         traceback.print_exc()
         self.terminate()
예제 #8
0
def local_ip_address():
    nics = psutil.net_if_addrs()
    primary_nic = None
    nic = None
    addr = None

    # Remove the loopback interface from the hash.  If this results in an
    # empty hash, return None.
    del nics["lo"]
    if not nics:
        logging.debug("No network interfaces found.  That's weird.")
        return None

    # Search the hash for the primary NIC.
    for nic in list(nics.keys()):
        # Make sure we filter out VPN interfaces.
        if "tun" in nic:
            continue

        for addr in nics[nic]:
            # We want AF_INET.
            if addr.family == 2:
                # Only the primary has a broadcast address.
                if addr.broadcast:
                    primary_nic = addr

    # Return the IP address if we have one, an error message if not.
    if primary_nic:
        logging.debug("Got primary IP address of system: " +
                      primary_nic.address)
        return primary_nic.address
    else:
        logging.err("Unable to get primary IP address.  Something went wrong.")
        return "unknown.  Something went wrong"
예제 #9
0
def get_sqsmessage():
    """ gets a complaint message from the sqs queue
        in: sqs message, sqs message id
        return sqs message, sqs message id, sqs message receipt handle
    """
    try:
        sqs = session.client('sqs')
        response = sqs.receive_message(
            QueueUrl=queue_url,
            AttributeNames=['SentTimestamp'],
            MaxNumberOfMessages=1,
            MessageAttributeNames=['All'],
            VisibilityTimeout=600  # ten minutes
            #VisibilityTimeout=10 # ten seconds
        )
    except:
        err("problem reading queue {}".format(pp['queue_name']))
        return 'error', 'error', 'error'
    else:
        # handle message
        if 'Messages' in response.keys():
            message = response['Messages'][0]
            return json.loads(message['Body']
                              ), message['MessageId'], message['ReceiptHandle']
        else:
            return None, None, None
예제 #10
0
def chown(user,group,p_raw,s=False,lc=False):
    """
    Unix chown. Changes the ownership of files, directories, or a list of files and
    directories to that of user and group, but does not recursively act on directories.
    
    Pre-conditions: user and group must be valid users and groups. p_raw must be a valid 
    file, directory, or list of files and directories.
    Chown must be run as root.
    
    Examples:
        chown("joe", "staff", "file.txt")
            changes UID and GID of file.txt to joe and staff respectively.
        chown("joe", "staff", ["file.txt", "A_folder", "script.py"])
            changes UID and GID of file.txt, A_folder, and script.py to joe and staff respectively.
    Possible Errors:
        not root - does nothing
        invalid user - does nothing
        invalid group - does nothing
        invalid file or directory name - does nothing.
    
    """
    clean_list = clean_flist(p_raw,s=s)
    try:
        uid = getpwnam(user).pw_uid
        debug(uid)
    except KeyError, k:
        if (s or STRICT):
            raise k
        else:
            err("No such username [%s]" % user)
        return
예제 #11
0
def network_traffic(openwrt_host):
    logging.debug("Entered function openwrt.network_traffic().")

    request = None
    nics = {}
    stats = {}

    # Get the list of physical NICs on the OpenWRT host.
    request = requests.get(openwrt_host + "/cgi-bin/network/device")
    if not request:
        logging.err("Failed to contact endpoint " + openwrt_host +
                    "/cgi-bin/network/device")
        return None
    nics = json.loads(request.text)
    del nics["lo"]

    # Prime the network stats hash table with the remaining network
    # interfaces.  We make sure the network interfaces are marked as up.
    for i in list(nics.keys()):
        if nics[i]["up"]:
            stats[i] = {}

    # For every online interface, convert rx_bytes and tx_bytes into
    # human-readable strings.
    for i in list(nics.keys()):
        stats[i]["sent"] = convert_bytes(nics[i]["statistics"]["tx_bytes"])
        stats[i]["received"] = convert_bytes(nics[i]["statistics"]["rx_bytes"])
        logging.debug("Traffic volume to date for " + i + ": " + str(stats[i]))

    return stats
예제 #12
0
def grep(regex, p_raw,s=False,lc=False):
    """
        Search a file or list of files for the specified regex or list of
        regexes, returning all matching lines.
    """
    clean_src = clean_flist(p_raw,s=s)
    results = []
    if type(regex) == type(list()):
        regex_list = regex
    else:
        regex_list = [regex]
        
    for src in clean_src:
        try:
            fh = open(src)
        except Exception, e:
            if (s or STRICT):
                raise e
            else:
                perror("Cannot open file [%s]." %src)
                err("Cannot open file [%s]." %src)
            continue
        for line in fh:
            for re_tmp in regex_list:
                if re.search(re_tmp, line) != None:
                    results.append(line)
                    continue
        fh.close()
예제 #13
0
 def hook(self, obj):
     # transform to snakecase if possible
     try:
         return key_convert(obj, humps.decamelize)
     except ValueError as err:
         logging.err(str(err))
         return super().hook(obj)
예제 #14
0
def ThreadRequest(asins, settings, products, sema, err):
    """
    Function to send query to keepa and store results

    Supports threads
    
    """

    # Attempt request
    try:
        try:
            response = ProductQuery(asins, settings)
            products.extend(response['products'])
        except Exception as e:
            logging.warning('Exception {:s} in thread.  Waiting 60 seconds for retry.'.format(e))
            time.sleep(60)
            
            # Try again
            response = ProductQuery(asins, settings)
            products.extend(response['products'])
            
    except:
        # Track error
        err = True

    # Log
    if not err:
        logging.info('Completed {:d} ASIN(s)'.format(len(products)))
    else:
        logging.err('Request failed')
        
    # finally, release thread
    sema.release()
예제 #15
0
def test_db():
    try: pinners = Table('pinners', metadata, autoload=True)
    except sqlalchemy.exc.NoSuchTableError:
        war("no pinners table")
        create_tables()
        load_tables()
        try: pinners = Table('pinners', metadata, autoload=True)
        except:
            err("could not open pinners table")
            exit(-1)
    contents = Table('contents', metadata, autoload=True)
    complaints = Table('complaints', metadata, autoload=True)
    reviewers = Table('reviewers', metadata, autoload=True)
    
    rs = pinners.select()
    run(rs, text='pinners')

    rs = contents.select()
    run(rs, text='contents')
    
    rs = complaints.select()
    run(rs, text='complaints')
    
    rs = reviewers.select()
    run(rs, text='reviewers')
    
    rs = join(pinners, contents).select()
    run(rs, text='join contents')
    
    rs = select([pinners.c.name, contents.c.url], and_(pinners.c.name == 'Mary', pinners.c.pinner_id == contents.c.pinner_id))
    run(rs, text='Mary contents')
    
    rs = select([pinners.c.pinner_id, contents.c.url, contents.c.content_id], \
        and_(pinners.c.pinner_id == 3, pinners.c.pinner_id == contents.c.pinner_id))
    run(rs, text='3 contents')
예제 #16
0
def rm(p_raw='', s=False, lc=False):
    """
    Unix rm. Will remove all regular files matching pattern p_raw (either a string or a list of
    strings). Does not remove directories - not an equivalent of rm -r.

    Pre-conditions: p_raw is a string or list of valid unix paths.
    Post-conditions: all regular files matching p_raw will be removed.
    Return value: none

    Examples:
    rm('home/file.txt')
        removes file file.txt from the subdirectory named home of the current directory (if the
        path home/file.txt exists)
    rm('*')
        removes everything in current directory

    Possible errors:
        no path provided - does nothing
        entry matching path is not a file - skips path
    """

    for entry in clean_flist(p_raw,s=s):
        if isfile(entry):
            remove(entry)
        else:
            if (s or STRICT):
                raise Exception, "Cannot remove [%s]: not a regular file." % entry 
            else:
                perror("Cannot remove [%s]: not a regular file." % entry)
                err("Cannot remove [%s]: not a regular file." % entry)
예제 #17
0
 def fib():
     hi = input(
         _("Would you like...\n    1 - Calculate a fixed amount of fibonacci numbers.\n    2 - Calculate fibonacci indefinitely.\nType: "
           ))
     if hi[0] == "1":
         cprint.info(_("Fixed it is."))
         from mathmod.fibonacci import CalculateFixedFibo
         amount = int(
             input(
                 _("How many numbers of fibonacci would you like to calculate? "
                   )))
         logging.info("About to run fixed fibonacci (amount=%s)" % amount)
         finalProduct = CalculateFixedFibo(amount)
         cprint.info(_("Your fibonacci numbers were..."))
         cprint.ok(finalProduct)
         logging.info(
             "User did fixed fibo with amount of %s, and the results are in the next log entry!..."
             % amount)
         logging.info(finalProduct)
     else:
         cprint.info(_("Looped it is."))
         from mathmod.fibonacci import CalculateLoopedFibo
         logging.info("About to run looped fibonacci")
         cprint.ok(_("Press Control-C to stop."))
         try:
             CalculateLoopedFibo()
         except Exception as ename:
             logging.err("Exception %s in looped fibonacci" % ename)
             cprint.err(_("An error occured."))
         except KeyboardInterrupt:
             logging.info("Exited fibonacci loop.")
     logging.info("User ran fibonacci function")
예제 #18
0
def uname(openwrt_host):
    logging.debug("Entered function openwrt.uname().")

    request = None
    sysinfo = {}
    system_info = {}

    #  Contact the OpenWRT host and get system info.
    request = requests.get(openwrt_host + "/cgi-bin/system/board")
    if not request:
        logging.err("Failed to contact endpoint " + openwrt_host +
                    "/cgi-bin/system/board")
        system_info["hostname"] = "ERROR - unknown"
        system_info["version"] = "ERROR - unknown"
        system_info["buildinfo"] = "ERROR - unknown"
        system_info["arch"] = "ERROR - unknown"
        return system_info
    sysinfo = json.loads(request.text)

    # Extract the info we want.
    system_info["hostname"] = sysinfo["hostname"]
    system_info["version"] = sysinfo["kernel"]
    system_info["buildinfo"] = sysinfo["release"]["description"]
    system_info["arch"] = sysinfo["system"] + " (" + sysinfo["release"][
        "target"] + ") (" + sysinfo["board_name"] + ")"
    return system_info
예제 #19
0
def assert_exists(fp,s=False,lc=False):
    if not exists(fp):
        if (s or STRICT):
            raise Exception, "[%s] does not exist"
        else:
            perror("[%s] does not exist")
            err("[%s] does not exist")
        sys.exit(-1)
예제 #20
0
파일: util.py 프로젝트: six-beauty/cousin
def cal_payment(male, child, ly_type, room):
    if ly_type == 1:
        payment = male * 108 + child * 55 + room * 100
    elif ly_type == 2:
        payment = male * 188 + child * 95 + room * 100
    else:
        logging.err('cal_payment fail, invalid ly_type:%s', ly_type)
    return payment
예제 #21
0
def cpu_idle_time(openwrt_host):
    logging.debug("Entered function openwrt.cpu_idle_time().")
    request = None

    request = requests.get(openwrt_host + "/cgi-bin/system/cpu_idle")
    if not request:
        logging.err("Failed to contact endpoint " + openwrt_host +
                    "/cgi-bin/system/cpu_idle")
        return 0.0
    return float(request.text)
예제 #22
0
 def __init__(self, keyword, conf):
   try:
     fileHandle = open(conf)
   except IOError:
     logging.err("Loading config file: %s error" %(conf))
     exit()
   self.conf = json.load(fileHandle)
   self.connection = Connection(self.conf['host'], self.conf['port'])
   self.db = self.connection[self.conf['dbname']]
   self.posts = self.db[keyword]
예제 #23
0
def convert(before, after):
    try:
        b_file = open(before, "r")
        a_file = open(after, "w")
    except IOError:
        logging.err("openfile error")
        exit()

    s = b_file.read()
    s = s.replace("indices", "")
    s = s.replace("text", "")
    a_file.write(s)
예제 #24
0
def convert(doc, jsonfile):
  try:
    fileHandle_text = open(doc, 'r')
    fileHandle_json = open(jsonfile, 'w')
  except IOError:
    logging.err("loading file error")
    exit()
  sss = fileHandle_text.read()
  sss = sss.replace('}{', '}\n{')
  fileHandle_json.write(sss)
  fileHandle_text.close()
  fileHandle_json.close()
예제 #25
0
파일: model.py 프로젝트: thruflo/london
 def mark_viewed(self, user, should_commit=True):
     session = Session()
     if not user in self.viewed:
         if should_commit:
             session.begin()
         self.viewed.append(user)
         session.add(self)
         if should_commit:
             try:
                 session.commit()
             except IntegrityError, err:
                 logging.err(err)
                 session.rollback()
예제 #26
0
파일: plugin.py 프로젝트: nickthename/pibot
 def load_plugin(self, name, info):
     mod = imp.load_module(name, *info)
     try:
         modname = mod.NAME
         if modname in self.plugins:
             l.warn("Module", modname, "already loaded. Ignoring duplicate")
         else:
             l.info("Loading Module:", modname)
             plugin = mod.init(self.bundle)
             self.plugins[modname] = plugin
     except Exception as e:
         l.err("Error loading module", name)
         l.err('\t', str(e))
예제 #27
0
 def unfavourite(self, user, should_commit=True):
     session = Session()
     if user in self.favourites:
         if should_commit:
             session.begin()
         self.favourites.remove(user)
         session.add(self)
         if should_commit:
             try:
                 session.commit()
             except IntegrityError, err:
                 logging.err(err)
                 session.rollback()
예제 #28
0
 def mark_viewed(self, user, should_commit=True):
     session = Session()
     if not user in self.viewed:
         if should_commit:
             session.begin()
         self.viewed.append(user)
         session.add(self)
         if should_commit:
             try:
                 session.commit()
             except IntegrityError, err:
                 logging.err(err)
                 session.rollback()
예제 #29
0
파일: model.py 프로젝트: thruflo/london
 def unfavourite(self, user, should_commit=True):
     session = Session()
     if user in self.favourites:
         if should_commit:
             session.begin()
         self.favourites.remove(user)
         session.add(self)
         if should_commit:
             try:
                 session.commit()
             except IntegrityError, err:
                 logging.err(err)
                 session.rollback()
예제 #30
0
def bz2(src, dst, s=False ,lc=False):
    try:
        dst_fh = py_bz2.open(dst,"w")
        src_fh = open(src,"r")
        dst_fh.write(src_fh.read())
        dst_fh.close()
        src_fh.close()
    except Exception, e:
        if (s or STRICT):
            raise e
        else:
            perror("%s" %e)
            err("%s" %e)
예제 #31
0
    def item_completed(self, results, item, info):
        if self.LOG_FAILED_RESULTS:
            msg = '%s found errors proessing %s' % (self.__class__.__name__, item)
            for ok, value in results:
                if not ok:
                    log.err(value, msg, spider=info.spider)

        image_paths = [x['path'] for ok, x in results if ok]
        image_path = list_first_item(image_paths)
        item['book_covor_image_path'] = os.path.join(os.path.abspath(self.images_store),
                                                     image_path) if image_path else ""

        return item
예제 #32
0
def send_message(chat_id,text):
    p=Setting.objects.filter(valuename="TELETOKEN")
    token =p.values()[0]['value']
    del p
    url="https://api.telegram.org/bot"+token+'/sendMessage'
    answer={'chat_id':chat_id, 'parse_mode':'HTML','text':text}
    try:
        r=requests.post (url,data=answer)
    except:
        logging.err( 'ERR in send' )
    else:
        logging.info('SEND '+ r.text )
        return r.json()
    return '1' 
예제 #33
0
def local_ip_address(openwrt_host):
    logging.debug("Entered function openwrt.local_ip_address().")

    request = None
    nics = []
    primary_nic = None
    address = ""

    # Get a list of logical NICs on the host.
    nics = get_logical_interfaces(openwrt_host)
    if not nics:
        logging.warn(
            "Couldn't get a list of network interfaces.  That's weird.")
        return None

    # Iterate through the list of network interfaces.
    for nic in nics:
        #  Contact the OpenWRT host and poll the interface.
        request = requests.get(openwrt_host + "/cgi-bin/network/interface?" +
                               nic)
        if not request:
            logging.err("Failed to contact endpoint " + openwrt_host +
                        "/cgi-bin/network/interface?" + nic)
            continue
        primary_nic = json.loads(request.text)

        # There is an assumption here which seems to hold for OpenWRT v18.04:
        # The WAN interface is the only one which has a primary_nic.route[] list
        # with a non-zero number of entries.  The semantics seem to be "the
        # default route."
        if len(primary_nic.route):
            logging.debug("Found primary network interface " + nic + ".")
            break
        else:
            logging.debug("The network interface " + nic +
                          " does not seem to have the default route.")
            primary_nic = None

    # If primary_nic is still None, we didn't find any interfaces with a
    # default route.
    if not primary_nic:
        logging.warning(
            "Did not find a network interface with a default route.  This is really strange."
        )
        return None

    # Extract the IP address of the primary NIC.
    address = primary_nic["route"][0]["source"].split("/")[0]
    logging.debug("I think the primary network interface is " + address + ".")
    return address
예제 #34
0
def highlight_stalls(
    file_path,
    target_codelet,
    target_tile,
    print_allowed=False,
):
    found_stall = False
    highlight_command = False
    count_up_empty_command = 0

    if not target_tile and not target_codelet:
        logging.err(
            'At least one out of following options shall be set: codelet, tile'
        )
        exit(1)

    with open(file_path, mode='r', encoding='utf-8') as f:
        lines = f.readlines()
        for i in range(len(lines)):
            codelet_matched = False
            tile_matched = False
            match = sim_line_match.match(lines[i])

            if match:
                data_fields = sim_code_data._make(x for x in match.groups())
                if not target_tile and data_fields.tile.find(
                        target_tile) != -1:
                    tile_matched = True

                if not target_codelet and data_fields.codelet.find(
                        target_codelet) != -1:
                    codelet_matched = True

                if codelet_matched or tile_matched:
                    if data_fields.command.strip() is '-':
                        highlight_command = True
                        count_up_empty_command += 1
                        custom_log(lines[i][:-1], print_allowed, bcolors.RED)
                    elif highlight_command is True:
                        highlight_command = False
                        # Presence of the 6 empty commands is a marker for a register stall
                        if count_up_empty_command is 6:
                            found_stall = True
                            count_up_empty_command = 0
                        custom_log(lines[i][:-1], print_allowed,
                                   bcolors.YELLOW)
                    else:
                        custom_log(lines[i][:-1], print_allowed)

    return found_stall
예제 #35
0
    def verify_response_basic(self):
        j = None
        try:
            j = json.loads(self.data)
        except:
            self.set_err(ErrCode.DATA_INVALID, "fail to load response "
                         "data to json")
            return False
        if not isinstance(j, dict):
            self.set_err(ErrCode.DATA_INVALID, "data should be aa dict type")
            return False
        if 'medias' not in j:
            self.set_err(ErrCode.DATA_INVALID, "no medias tag found")
            return False

        if not 'background' in j:
            self.set_err(ErrCode.DATA_INVALID,
                         "the background image is absent")
            logging.err("the number of the background image of the topic "
                        "%d is absent" % self.id)

        succ, msg = validate_pic(j['background'])
        if succ is False:
            self.set_err(ErrCode.BAD_DETAIL_VIEW_IMAGE, "topic(%s) background "
                         "image cannot be loaded successfully, err(%s)"%\
                         (media['title'], msg))
            logging.error("topic(%s) background "
                         "image cannot be loaded successfully, err(%s)"%\
                         (media['title'], msg))
            return False

        medias = j['medias']
        if len(medias) == 0:
            self.set_err(ErrCode.DATA_INVALID,
                         "the number of media of this topic is 0")
            logging.error("the number of media is 0 for topic %s" % self.id)
            return False

        min_num = 2
        if len(medias) < min_num:
            self.set_err(
                ErrCode.DATA_INVALID,
                "the number of media of this topic is less than "
                "%d, actual %d" % (min_num, len(medias)))
            logging.error("the number of media of topic %s is less than "
                          "%d, actual %d" % (self.id, min_num, len(medias)))
            return False
        logging.debug("topic %s has %s medias found" % (self.id, len(medias)))

        return True
예제 #36
0
    def verify_response_basic(self):
        j = None
        try:
            j = json.loads(self.data)
        except:
            self.set_err(ErrCode.DATA_INVALID, "fail to load response "
                         "data to json")
            return False
        if not isinstance(j, dict):
            self.set_err(ErrCode.DATA_INVALID, "data should be aa dict type")
            return False
        if 'medias' not in j:
            self.set_err(ErrCode.DATA_INVALID, "no medias tag found")
            return False

        if not 'background' in j:
            self.set_err(ErrCode.DATA_INVALID,
                         "the background image is absent")
            logging.err("the number of the background image of the topic "
                        "%d is absent"%self.id)

        succ, msg = validate_pic(j['background'])
        if succ is False:
            self.set_err(ErrCode.BAD_DETAIL_VIEW_IMAGE, "topic(%s) background "
                         "image cannot be loaded successfully, err(%s)"%\
                         (media['title'], msg))
            logging.error("topic(%s) background "
                         "image cannot be loaded successfully, err(%s)"%\
                         (media['title'], msg))
            return False

        medias = j['medias']
        if len(medias) == 0:
            self.set_err(ErrCode.DATA_INVALID,
                         "the number of media of this topic is 0")
            logging.error("the number of media is 0 for topic %s"%self.id)
            return False
        
        min_num = 2
        if len(medias) < min_num:
            self.set_err(ErrCode.DATA_INVALID,
                         "the number of media of this topic is less than "
                         "%d, actual %d"%(min_num, len(medias)))
            logging.error("the number of media of topic %s is less than "
                          "%d, actual %d"%(self.id, min_num, len(medias)))
            return False
        logging.debug("topic %s has %s medias found"%(self.id, len(medias)))
            
        return True
예제 #37
0
def mkdir(p_raw,s=False,lc=False):
    """
    Unix mkdir, but makes all intermediate directories as well if they don't already exist.

    Pre-conditions: p_raw is a string or list of strings of valid unix paths.
    Post-conditions: all directories along paths matching p_raw will be created.
    Return value: none

    Examples:
    mkdir(['~/docs','~/tmp'])
        makes directories 'docs' and 'tmp' in user's home directory
    mkdir('files/img')
        will create a directory 'files' in current directory if it doesn't exist, and create
        the directory 'img' inside of that.

    Possible errors:
        no path provided - does nothing
        python is unable to create a directory - skips path
        directory already exists - skips path
    """

    if not p_raw:
        if (s or STRICT):
            raise Exception, "Missing file argument in mkdir."
        else:
            perror("Missing file argument in mkdir.")
            err("Missing file argument in mkdir.")
        return

    if type(p_raw) != type(list()):
        p_raw  = [p_raw]

    for dp in p_raw:
        full_dp = expand(dp)
        if not exists(full_dp):
            try:
                makedirs(full_dp)
            except Exception, e:
                if (s or STRICT):
                    raise e
                else:
                    perror("Cannot make directory [%s]" % full_dp)
                    err("Cannot make directory [%s]" % full_dp)
        else:
            if (s or STRICT):
                raise Exception, "Directory [%s] exists" % full_dp
            else:
                #pinfo("Directory [%s] exists - skipping mkdir" % full_dp)
                info("Directory [%s] exists - skipping mkdir" % full_dp)
예제 #38
0
def uptime(openwrt_host):
    logging.debug("Entered function openwrt.uptime().")

    request = None
    uptime_seconds = {}

    #  Contact the OpenWRT host and get system info.
    request = requests.get(openwrt_host + "/cgi-bin/system/info")
    if not request:
        logging.err("Failed to contact endpoint " + openwrt_host +
                    "/cgi-bin/system/info")
        return None
    uptime_seconds = json.loads(request.text)
    uptime_seconds = uptime_seconds["uptime"]
    return str(timedelta(seconds=uptime_seconds))
예제 #39
0
def put_sqsmessage(comp):
    """ puts a complaint message in the sqs queue
        in: complaint json
        out: sqs message
        return sqs message id
    """
    try:
        sqs = session.resource('sqs')
        queue = sqs.get_queue_by_name(QueueName=queue_name)
        response = queue.send_message(MessageBody=json.dumps(comp))
    except:
        err("cannot send message to sqs")
        return 'error'
    else:
        return response.get('MessageId')
예제 #40
0
파일: util.py 프로젝트: six-beauty/cousin
def load_pay_imgs(imgs_dir):
    load_pay_imgs = []
    if not os.path.isdir(imgs_dir):
        logging.err('load_pay_imgs images fail, imgs_dir not a dir:%s',
                    imgs_dir)
        return load_pay_imgs

    imgs = os.listdir(imgs_dir)
    #过滤掉不合规则的
    for img in imgs:
        prefix = re.search('(\d+).png', img, re.DOTALL)
        if not prefix or len(prefix.group(1)) != 8:
            continue
        load_pay_imgs.append(prefix.group(1))
    return load_pay_imgs
예제 #41
0
    def item_completed(self, results, item, info):
        if self.LOG_FAILED_RESULTS:
            msg = '%s found errors proessing %s' % (self.__class__.__name__,
                                                    item)
            for ok, value in results:
                if not ok:
                    log.err(value, msg, spider=info.spider)

        image_paths = [x['path'] for ok, x in results if ok]
        image_path = list_first_item(image_paths)
        item['book_covor_image_path'] = os.path.join(
            os.path.abspath(self.images_store),
            image_path) if image_path else ""

        return item
예제 #42
0
def fsplit(in_fp, maxlines, maxsetcount=0, pad=3, prefix="", suffix="",s=False,lc=False):
    pattern = "%s%0" + str(pad) + "d%s"
    files   = []

    in_fp = expand(in_fp)

    try:
        in_fh   = open(in_fp)
    except Exception, e:
        if (s or STRICT):
            raise e
        else:
            perror("Cannot open file [%s]" %in_fp)
            err("Cannot open file [%s]" %in_fp)
        return
예제 #43
0
 def term_read(self, term_name):
     with self.lock:
         term = self.terminals.get(term_name)
         if not term:
             return
         try:
             data = os.read(term.fd, 65536)
             if not data:
                 logging.error("pyxshell: EOF in reading from %s; closing it" % term_name)
                 self.term_update(term_name)
                 self.kill_term(term_name)
                 return
             term.pty_read(data)
         except (KeyError, IOError, OSError):
             logging.err("pyxshell: Error in reading from %s; closing it" % term_name)
             self.kill_term(term_name)
예제 #44
0
def head(src, num_lines=10,s=False,lc=False):
    """
    Unix head. Returns the first 10 lines of a file in a list. The number
    of lines printed can also be defined with num_lines.

    Pre-conditions: src must be a valid file. If num_lines is defined,
    it must be a positive number.

    Example:
    head("script.py")
        returns the first 10 lines of the file in a list.
    head("script.py", num_lines=3)
        returns the first 3 lines of the file in a list.

    Possible errors:
        if no file is provided - does nothing
        negative num_line is provided - does nothing
    """
    
    clean_src = clean_flist(src,s=s)
    lines = []

    if num_lines < 0:
        if (s or STRICT):
            raise Exception, "Negative number of lines"
        else:
            perror("Negative number of lines")
            err("Negative number of lines")
        return

    for f in clean_src:
        try:
            fh = open(f,"r")
            for i in range(0, num_lines):
                line = fh.readline()
                if line:
                    lines.append(line)
                else:
                    break
            fh.close()
        except Exception, e:
            if (s or STRICT):
                raise e
            else:
                perror("Could not read file [%s] - skipping" % f)
                err("Could not read file [%s] - skipping" % f)
            debug("%s", e)
예제 #45
0
파일: order.py 프로젝트: six-beauty/cousin
    def send_mail(self,
                  name,
                  phone,
                  wx,
                  male,
                  child,
                  ly_type,
                  room,
                  order_date,
                  retry_time=3):
        if retry_time <= 0:
            logging.err('send_mail fail, phone:%s order_date:%s', phone,
                        order_date)
            return
        try:
            content = """
    -----------------begin---------------------------
    名字:{0}    \t,手机:{1}    \t,微信:{2}    \t,预约时间:{3}    \t,
    -----------------end----------------------------------------------------
    成人:{4}人    \t,儿童:{5}人    \t,预约类型:{6}日游    \t,预约房间:{7}房    \t,
    -----------------end----------------------------------------------------
    """.format(name, phone, wx, order_date, male, child, ly_type, room)
            msg = email.mime.multipart.MIMEMultipart()
            msg['From'] = py_conf['mail']['from_addr']
            msg['To'] = email.utils.COMMASPACE.join(py_conf['mail']['to_addr'])
            msg['Subject'] = '农庄预约订单'
            msg['Date'] = email.utils.formatdate(localtime=True)
            text = email.mime.text.MIMEText(content, 'plain', 'utf-8')
            msg.attach(text)

            self.smtp.sendmail(from_addr, to_addr, msg.as_string())
        except:
            logging.err('send_mail fail, %s', traceback.format_exc())
            self.smtp = smtplib.SMTP()
            self.smtp.connect(py_conf['mail']['from_svr'],
                              py_conf['mail']['svr_port'])
            self.smtp.login(py_conf['mail']['from_addr'],
                            py_conf['mail']['from_passwd'])

            name = name.decode('utf-8')
            phone = phone.decode('utf-8')
            wx = wx.decode('utf-8')
            self.send_mail(name, phone, wx, male, child, ly_type, room,
                           order_date, retry_time - 1)
예제 #46
0
 def term_read(self, term_name):
     with self.lock:
         term = self.terminals.get(term_name)
         if not term:
             return
         try:
             data = os.read(term.fd, 65536)
             if not data:
                 logging.error(
                     "pyxshell: EOF in reading from %s; closing it" %
                     term_name)
                 self.term_update(term_name)
                 self.kill_term(term_name)
                 return
             term.pty_read(data)
         except (KeyError, IOError, OSError):
             logging.err("pyxshell: Error in reading from %s; closing it" %
                         term_name)
             self.kill_term(term_name)
예제 #47
0
def cat(src, dst=None, append=False, s=False, lc=False):
    """
    Unix cat. Returns the contents of a file or list of files.
 
    Pre-conditions: src is a string or a list of valid unix paths to files.
    Post-conditions: none
    Return value: a list in which each entry is a line, terminated by a newline, of
    the files in src

    Example:
    cat("info.txt")
        returns a list of the contents of info.txt: ['line 1\n','line 2\n'...]

    Possible errors:
        an entry in src is a directory - skips entry
        cannot open an entry in src - skips entry
    """
    lines = []
    
    files = clean_flist(src, s=s)
    
    if dst:
        fixpath(dst)
        if append:
            out_fh = open(dst,'a')
        else:
            out_fh = open(dst,'w')
        
    for f in files:
        try:
            fh = open(f)
            if dst:
                out_fh.write(fh.read())
            else:
                lines.extend(fh.readlines())
            fh.close()
        except Exception, e:
            if (s or STRICT):
                raise e
            else:
                perror("Cannot read file [%s]" % file)
                err("Cannot read file [%s]" % f)
예제 #48
0
def get_disk_usage(openwrt_host):
    logging.debug("Entered function openwrt.get_disk_usage().")

    # "root" is /dev/root, which is always at 100%
    # "ubi" corresponds to sections of nvram that are overlaid onto /root,
    #   which hold system config variables.
    ignored = ["root", "ubi"]

    request = None
    raw_storage_stats = ""
    tmp_storage_stats = []
    storage_stats = []
    disk_used = {}

    # Get the disk usage stats from the OpenWRT device.
    request = requests.get(openwrt_host + "/cgi-bin/system/storage")
    if not request:
        logging.err("Failed to contact endpoint " + openwrt_host +
                    "/cgi-bin/system/storage")
        disk_used["/"] = "unsupported"
        return disk_used
    raw_storage_stats = request.text
    tmp_storage_stats = raw_storage_stats.splitlines()

    # Get rid of the empty lines.  I can never remember how to do this..
    tmp_storage_stats = [x for x in tmp_storage_stats if x]
    for i in tmp_storage_stats:
        storage_stats.append(i.split())

    # Remove the lines in the array that we don't care about.
    for i in storage_stats:
        for j in ignored:
            if j in i[0]:
                storage_stats.remove(i)
                break
    logging.debug("Value of storage_stats: " + str(storage_stats))

    # Build the disk_used hash.
    for i in storage_stats:
        disk_used[i[-1]] = float(i[-2].replace("%", ""))
    logging.debug("Value of disk_used: " + str(disk_used))
    return disk_used
예제 #49
0
def c(cmd, stdin=None, stdout=PIPE, stderr=PIPE, aslist=True):
    "Execute cmd through the shell and return list of lines of output, unless aslist==False"
    if cmd == "" or cmd == None:
        return
    exp_cmd = []
    for p in cmd.split(" "):
        # FIXME: We'd like to do glob expansion, but this only works for
        #       arguments that are files that exist, otherwise the argument
        #       just disappears
        #exp_cmd.extend(glob(expand(p)))
        exp_cmd.append(expand(p))

    exp_cmd[0] = findFile(exp_cmd[0],os.environ['PATH'])
    if exp_cmd[0] == None or not exists(exp_cmd[0]):
        err("executable [%s] not found in current directory or in PATH (orig cmd: [%s])" % (exp_cmd[0], cmd))
        debug("PATH: [%s]" % "\n".join(os.environ['PATH'].split(":")))
        return

    mode = os.stat(exp_cmd[0])[0]
    # check if execute bit is set for at least one of USR, GRP, or OTH
    if not (stat.S_IXUSR & mode or stat.S_IXGRP & mode or stat.S_IXOTH & mode):
        # attempt to make the file executable
        info("file is not executable [%s] mode [%s] -- attempting chmod a+x" % (exp_cmd[0], mode))
        try:
            chmod("a+x",exp_cmd[0])
        except:
            err("chmod a+x [%s] failed -- execution will likely fail")

    #(status, raw_output) =  getstatusoutput(cmd)
    debug("starting process: [%s]" % " ".join(exp_cmd))
    proc = Popen(exp_cmd, stdin=stdin, stdout=stdout, stderr=stderr)
    (stdout, stderr) = proc.communicate()

    if stderr != None and stderr != "":
        perror(stderr)

    if aslist:
        output = stdout.split("\n")
    else:
        output = stdout
    return output
예제 #50
0
def tail(src, num_lines=10,s=False,lc=False):
    """
    Unix tail. Returns the last ten lines of a file in a list. The number
    of lines can also be defined by using num_lines.
    
    Pre-conditions: src must be a valid file. If num_lines is defined,
    it must be a positive number
    
    Examples:
        tail("script.py")
            returns the last ten lines of a file in a list
        tail("script.py", num_lines="3")
            returns the last three lines of a file in a list
    """
    clean_src = clean_flist(src,s=s)
    lines = []

    if num_lines < 0:
        if (s or STRICT):
            raise Exception,"Negative number of lines"
        else:
            perror("Negative number of lines.") 
            err("Negative number of lines.") 
        return
    
    for f in clean_src:
        try:
            fh = open(f,"r")
            cur_lines = fh.readlines()
            fh.close()
        except Exception, e:
            if (s or STRICT):
                raise e
            else:
                perror("Cannot read file [%s] - skipping" % f)
                err("Cannot read file [%s] - skipping" % f)

        if len(cur_lines) <= num_lines:
            lines.extend(cur_lines)
        else:
            lines.extend(cur_lines[-1*num_lines:])
예제 #51
0
 def test_connection(self):
     logging.basicConfig(level=logging.INFO)
     r = []
     try:
         robot = urx.Robot("192.168.56.102")
         r = robot
         r.set_tcp((0,0,0,0,0,0))
         r.set_payload(0.5, (0,0,0))
         logging.info("Robot object is available as robot or r")   
         j = r.getj()
         print("Initial joint configuration is ", j) 
         p = r.get_pos()
         print("Initial TCP pos is ", p) 
         p = r.getl()
         print("Initial TCP pose is ", p)       
      
     except Exception as e:
         logging.err(str(e))
     
     finally:            
         r.close()
예제 #52
0
def unzip(zip_fp, dst=".", ext=".zip",s=False,lc=False):
    zip_fp = expand(zip_fp)

    if not exists(zip_fp):
        if (s or STRICT):
            raise Exception, "File [%s] not found" % zip_fp
        else:
            err("File [%s] not found" % zip_fp)
        return
    if not zip_fp.endswith(ext):
        if (s or STRICT):
            raise Exception, "gunzip: File [%s] expected to have [%s] extension" % (zip_fp, ext)
        else:
            err("gunzip: File [%s] expected to have [%s] extension" % (zip_fp, ext))
        return

    try:
        zip_obj      = zipfile.ZipFile(zip_fp, 'r')
        for m in zip_obj.infolist():
            zip_obj.extract(m,dst) # FIXME even this is Python2.6 only
        # require using ZipInfo obj file name, create dir, open file to write
        # and then extract_fh.write(zip_obj.read(m))
        # zip_obj.extractall() Python 2.6 onwards
    except Exception, e:
        if (s or STRICT):
            raise e
        else:
            perror("Cannot gunzip file [%s] [%s]" %(zip_fp,e))
            err("Cannot gunzip file [%s] [%s]" %(zip_fp,e))
        return
예제 #53
0
def gunzip(zip_fp, dst=".", ext=".gz",s=False,lc=False):
    zip_fp = expand(zip_fp)

    if not exists(zip_fp):
        if (s or STRICT):
            raise Exception, "File [%s] not found" % zip_fp
        else:
            perror("File [%s] not found" % zip_fp)
            err("File [%s] not found" % zip_fp)
        return
    if not zip_fp.endswith(ext):
        if (s or STRICT):
            raise Exception, "gunzip: File [%s] expected to have [%s] extension" % (zip_fp, ext)
        else:
            perror("gunzip: File [%s] expected to have [%s] extension" % (zip_fp, ext))
            err("gunzip: File [%s] expected to have [%s] extension" % (zip_fp, ext))
        return

    ext_idx  = zip_fp.find(ext)
    unzip_fp = zip_fp[0:ext_idx]

    try:
        zip_fh      = py_gzip.open(zip_fp, 'r')
        unzip_fh    = open("%s/%s" % (dst, unzip_fp), 'w')
        unzip_fh.write(zip_fh.read())
        unzip_fh.close()
        zip_fh.close()
    except Exception, e:
        if (s or STRICT):
            raise e
        else:
            perror("Cannot gunzip file [%s] [%s]" %(zip_fp,e))
            err("Cannot gunzip file [%s] [%s]" %(zip_fp,e))
        return
예제 #54
0
def memory_utilization(openwrt_host):
    logging.debug("Entered function openwrt.memory_utilization().")

    request = None
    memory_info = {}
    memory_stats = {}

    #  Contact the OpenWRT host and get system info.
    request = requests.get(openwrt_host + "/cgi-bin/system/info")
    if not request:
        logging.err("Failed to contact endpoint " + openwrt_host +
                    "/cgi-bin/system/info")
        memory_stats["free"] = "ERROR - unknown"
        memory_stats["buffers"] = "ERROR - unknown"
        memory_stats["cached"] = "ERROR - unknown"
        return memory_stats

    memory_info = json.loads(request.text)
    memory_stats["free"] = memory_info["memory"]["free"]
    memory_stats["buffers"] = memory_info["memory"]["buffered"]
    memory_stats["cached"] = memory_info["memory"]["shared"]
    return memory_stats
예제 #55
0
def get_logical_interfaces(openwrt_host):
    logging.debug("Entered function openwrt.get_logical_interfaces().")

    request = None
    interfaces = {}
    nics = []

    #  Contact the OpenWRT host and dump the logical network interfaces
    request = requests.get(openwrt_host + "/cgi-bin/network/interface?dump")
    if not request:
        logging.err("Failed to contact endpoint " + openwrt_host +
                    "/cgi-bin/network/interface?dump")
        return None
    interfaces = json.loads(request.text)

    # Iterate through the interfaces and pick out all the ones that aren't the
    # loopback.
    for i in interfaces["interface"]:
        if i["interface"] != "loopback":
            nics.append(i["interface"])
    logging.debug("List of interfaces found: " + str(nics))
    return nics
예제 #56
0
    def _generate_random_data_map(self, degree_distribution, segment, red, initial_xor_map=[], undecoded_elements=[]):
        """
        This is used for simulation only
        :param degree_distribution:
        :param segment:
        :param red:
        :param initial_xor_map:
        :param undecoded_elements:
        :return:
        """
        if degree_distribution == "old":
            xored_map = self._get_droplet_details_old(segment, math.ceil(segment * red))
        else:
            xored_map = self._get_droplet_details(segment, math.ceil(segment * red), initial_xor_map,
                                                  undecoded_elements)

        data_map = {}
        for i in range(len(xored_map)):
            try:
                data_map[tuple(sorted(xored_map[i]))] = format(i + 1, '0' + str(self.number_of_bit_per_origami) + 'b')
            except KeyError as e:
                logging.err("Missing origami: {i}".format(i=i))
        return data_map, xored_map
예제 #57
0
def curl(url, fn=None, s=False, lc=False):
    """
    Unix curl/wget. Downloads a file/direcroty from an url.

    Pre-conditions: url is a valid url
    Post-conditions: file will be downloaded and stored at the location fn. If fn is
    an existing directory, the contents will be stored under the basename of the url in that 
    directory. Otherwise, a new file will be created to hold those contents.
    Return value: none

    Examples:
    curl("http://www.example.com/css/example.css")
        stores the file example.css in the current directory
    curl("http://www.mysite.org/facts.txt", "info")
        if 'info' is a directory, stores 'facts.txt' in info. Otherwise, stores 'facts.txt' 
        in a file named 'info' in the current directory.

    Possible errors:
        unable to retrieve from url (e.g. invalid url) - does nothing
    """
    if fn == None or isdir(fn):
        url_parts = urlparse(url)
        if fn == None:
            fn = basename(url_parts.path)
        else:
            fn = "%s/%s" % (fn, basename(url_parts.path))

    debug("urlretrieve(%s, %s)" % (url, fn))
    try:
        urlretrieve(url, fn)
    except Exception, e:
        if (s or STRICT):
            raise e
        else:
            perror("Cannot retrieve file from url [%s]." % (url))
            err("Cannot retrieve file from url [%s]." % (url))
예제 #58
0
def cp(src, raw_dst,s=False,lc=False):
    """
    Unix cp -r. Copies a file, directory, or lists of files and directories to raw_dst. 

    Pre-conditions: src must be a string or list of valid unix paths that can expand to any number of 
    entries, but if it expands to more than one, raw_dst must be a directory. raw_dst may contain wildcards,
    but must only expand to one value.
    Post-conditions: all existing files/directories from src are copied to dst
    Return value: none

    Examples:
    cp('docs','docs_backup')
        recursively copies all files from docs to a new directory called docs_backup, unless docs_backup
        already exists, in which case files will be copied to docs_backup/docs
    cp('*.png','img')
        copies all files in the current directory with extension png to existing directory img
    
    Possible errors:
        copying multiple files or a directory to a single file - does nothing
        raw_dst expands to multiple locations - does nothing
        copying a directory to a target that exists - skips entry
        python copy function fails - skips entry
    """

    dst = expand(raw_dst)

    if type(dst) == type(list()):
        if (s or STRICT):
            raise Exception, "Invalid cp destination [%s]" %raw_dst
        else:
            perror("Invalid cp destination [%s]" %raw_dst)
            err("Invalid cp destination [%s]" %raw_dst)
        return

    clean_src = clean_flist(src,s=s)

    # Can only copy multiple sources to dst if dst is an existing directory, otherwise fail
    if len(clean_src) > 1 and not isdir(dst):
        if (s or STRICT):
            raise Exception, "[%s] is not a directory, cannot copy multiple entries" % dst
        else:
            perror("[%s] is not a directory, cannot copy multiple entries" % dst)
            err("[%s] is not a directory, cannot copy multiple entries" % dst)
    else:
        for item in clean_src:
            debug("atomic cp %s %s" % (item, dst))
            if isfile(item):
                try:
                    copy2(item,dst)
                except Exception, e:
                    if (s or STRICT):
                        raise e
                    else:
                        warning("Copy failed: %s %s %s" % (pwd(), item, dst))
            elif isdir(item):
                if isfile(dst):
                    if (s or STRICT):
                        raise Exception, "Cannot copy directory [%s] to file [%s]" % (item, dst)
                    else:
                        perror("Cannot copy directory [%s] to file [%s]" % (item, dst)) 
                        err("Cannot copy directory [%s] to file [%s]" % (item, dst)) 
                elif isdir(dst):
                    dir_dst = "%s/%s" % (dst, basename(item))
                    if not exists(dir_dst):
                        try:
                            copytree(item, dir_dst)
                        except Exception, e:
                            if (s or STRICT):
                                raise e
                            else:
                                warning("Copy failed: %s %s %s" % (pwd(), item, dir_dst))
                    else:
                        if (s or STRICT):
                            raise Exception, "[%s] destination already exists" % (dir_dst)
                        else:
                            perror("[%s] destination already exists" % (dir_dst))
                            err("[%s] destination already exists" % (dir_dst))
                else:
예제 #59
0
def pushd(dp,s=False,lc=False):
    """
    Unix pushd. Pushes current directory onto the global variable dirstack and changes
    directory to the new directory dp. If dp does not exist, pushd attempts to create it.

    Pre-conditions: dp is a single valid path name. It may contain wildcards and variables
    but only if they expand to a single path. 
    Post-conditions: if dp did not exist, it now does. The current directory is dp, and the
    top of dirstack is the previous directory.
    EDIT: no longer creates the directories - if directory doesn't exist, prints an error
    message
    Return value: none

    Examples:
    pushd('$HOME')
        pushes current directory and changes directory to the home directory
    pushd('..')
        pushes current directory and changes directory to the parent directory

    Possible errors:
        dp does not exist and python can't create it - does nothing
        cannot cd into directory (e.g. not world-executable) - does nothing
        dp expands to 0 or multiple paths - does nothing
    """
    expand_dp = clean_flist(dp,s=s)
    if len(expand_dp) > 1:
        if (s or STRICT):
            raise Exception, "Invalid path [%s]" % dp
        else:
            perror("Invalid path [%s]" % dp)
            err("Invalid path [%s]" % dp)
        return
    elif len(expand_dp)==0:
        if (s or STRICT):
            raise Exception, "No such file or directory [%s]" % dp
        else:
            warning("No such file or directory [%s]" % dp)
        return

    dp = expand_dp[0]

#   if not exists(dp):
#       try: 
#           makedirs(dp)
#       except Exception, e:
#           if (s or STRICT):
#               raise e
#           else:
#               perror("Cannot create directory [%s]." % dp)  
#               err("Cannot create directory [%s]." % dp)  
#           return 
    info("%s -> %s" % (pwd(), dp))

    try:
        cur = pwd() 
        chdir(dp)
        dirstack.append(cur)
    except Exception, e:
        if (s or STRICT):
            raise e
        else:
            perror("Cannot pushd [%s]." % dp) 
            err("Cannot pushd [%s]." % dp) 
예제 #60
0
def chmod(mode, p_raw,s=False,lc=False):
    """
    Unix chmod. Changes the mode of a file, directory, or list of files and directories, but 
    does not recursively act on directories.

    Pre-conditions: mode is a valid string matching the expression ^[ugoa]+[+-=][rwxX]+ and 
    p_raw is a valid string or list of unix paths
    Post-conditions: mode on all files/directories matching p_raw will be changed to add/
    subract permissions from certain users.
    Return value: none

    Examples:
    chmod("o-x","script.py")
        removes "execute" permission for "other" on file script.py
    chmod("u=rx","dir")
        sets permissions for "user" to "read" and "write"

    Possible errors:
        invalid mode - does nothing
    """
    clean_list = clean_flist(p_raw,s=s)
    
    for f in clean_list:
        cur_mode = os.stat(f).st_mode   
 
        ex = re.match("^(?P<ref>[ugoa]*)(?P<op>[+-=])(?P<perm>[rwxX]*)$",mode)
        if not ex:
            if (s or STRICT):
                raise Exception, "Invalid mode [%s]" % mode
            else:
                perror("Invalid mode [%s]" % mode)
                err("Invalid mode [%s]" % mode)
            return

        new_part = 0
        new_mode = cur_mode % 8**3

        for char in ex.group('perm'):
            debug(char)
            if char == 'r':
                new_part |= 4
            elif char == 'w':
                new_part |= 2
            elif char == 'x':
                new_part |= 1
            elif char == 'X':
                if isdir(f):
                    new_part |= 1
        debug("part: %o" %new_part)

        if ex.group('op') == '=':
            new_mode = 0
 
        u = new_part << 6
        g = new_part << 3
        o = new_part 
        a = u | g | o

        for char in ex.group('ref'):
            if ex.group('op') == '+' or ex.group('op') == '=':
                new_mode |= eval(char)
            elif ex.group('op') == '-':
                new_mode &= ~eval(char)

        debug("%o" % new_mode)
        os.chmod(f, new_mode)