예제 #1
0
 def testFileTimesTimezones(self):
     filename = tempfile.mktemp("-testFileTimes")
     # now() is always returning a timestamp with microseconds but the
     # file APIs all have zero microseconds, so some comparisons fail.
     now_utc = win32timezone.utcnow().replace(microsecond=0)
     now_local = now_utc.astimezone(win32timezone.TimeZoneInfo.local())
     h = win32file.CreateFile(
         filename,
         win32file.GENERIC_READ | win32file.GENERIC_WRITE,
         0,
         None,
         win32file.CREATE_ALWAYS,
         0,
         0,
     )
     try:
         win32file.SetFileTime(h, now_utc, now_utc, now_utc)
         ct, at, wt = win32file.GetFileTime(h)
         self.failUnlessEqual(now_local, ct)
         self.failUnlessEqual(now_local, at)
         self.failUnlessEqual(now_local, wt)
         # and the reverse - set local, check against utc
         win32file.SetFileTime(h, now_local, now_local, now_local)
         ct, at, wt = win32file.GetFileTime(h)
         self.failUnlessEqual(now_utc, ct)
         self.failUnlessEqual(now_utc, at)
         self.failUnlessEqual(now_utc, wt)
     finally:
         h.close()
         os.unlink(filename)
예제 #2
0
 def testFileTimesTimezones(self):
     if not issubclass(pywintypes.TimeType, datetime.datetime):
         # maybe should report 'skipped', but that's not quite right as
         # there is nothing you can do to avoid it being skipped!
         return
     filename = tempfile.mktemp("-testFileTimes")
     now_utc = win32timezone.utcnow()
     now_local = now_utc.astimezone(win32timezone.TimeZoneInfo.local())
     h = win32file.CreateFile(
         filename, win32file.GENERIC_READ | win32file.GENERIC_WRITE, 0,
         None, win32file.CREATE_ALWAYS, 0, 0)
     try:
         win32file.SetFileTime(h, now_utc, now_utc, now_utc)
         ct, at, wt = win32file.GetFileTime(h)
         self.failUnlessEqual(now_local, ct)
         self.failUnlessEqual(now_local, at)
         self.failUnlessEqual(now_local, wt)
         # and the reverse - set local, check against utc
         win32file.SetFileTime(h, now_local, now_local, now_local)
         ct, at, wt = win32file.GetFileTime(h)
         self.failUnlessEqual(now_utc, ct)
         self.failUnlessEqual(now_utc, at)
         self.failUnlessEqual(now_utc, wt)
     finally:
         h.close()
         os.unlink(filename)
예제 #3
0
 def testFileTimesTimezones(self):
     if not issubclass(pywintypes.TimeType, datetime.datetime):
         # maybe should report 'skipped', but that's not quite right as
         # there is nothing you can do to avoid it being skipped!
         return
     filename = tempfile.mktemp("-testFileTimes")
     now_utc = win32timezone.utcnow()
     now_local = now_utc.astimezone(win32timezone.TimeZoneInfo.local())
     h = win32file.CreateFile(filename,
                              win32file.GENERIC_READ|win32file.GENERIC_WRITE,
                              0, None, win32file.CREATE_ALWAYS, 0, 0)
     try:
         win32file.SetFileTime(h, now_utc, now_utc, now_utc)
         ct, at, wt = win32file.GetFileTime(h)
         self.failUnlessEqual(now_local, ct)
         self.failUnlessEqual(now_local, at)
         self.failUnlessEqual(now_local, wt)
         # and the reverse - set local, check against utc
         win32file.SetFileTime(h, now_local, now_local, now_local)
         ct, at, wt = win32file.GetFileTime(h)
         self.failUnlessEqual(now_utc, ct)
         self.failUnlessEqual(now_utc, at)
         self.failUnlessEqual(now_utc, wt)
     finally:
         h.close()
         os.unlink(filename)
예제 #4
0
 def on_before_message_send(self, queue, message):
     """
     Add scalarizr version to meta
     """
     message.meta.update({
         'szr_version': scalarizr.__version__,
         'timestamp': os_time.utcnow().strftime("%a %d %b %Y %H:%M:%S %z")
     })
예제 #5
0
파일: app.py 프로젝트: chenleji/scalarizr
 def msg_meta(queue, message):
     """
     Add scalarizr version to meta
     """
     message.meta.update({
         'szr_version': __version__,
         'timestamp': os_time.utcnow().strftime("%a %d %b %Y %H:%M:%S %z")
     })
예제 #6
0
    def handler(self, form):
        path = form.get("path", self.PATH)
        size_threshold_mb = int(
            form.get("size_threshold_mb", self.SIZE_THRESHOLD_MB) or 0)
        refresh_secs = int(form.get("refresh_secs", self.REFRESH_SECS) or 0)
        status = "Waiting"
        if path and fs.Dir(path):
            #
            # Ignore any non-existent paths, including garbage.
            # Create a new path handler if needed, or pull back
            # and existing one, and return the latest list.
            #
            with self._paths_lock:
                if path not in self.paths:
                    self.paths[path] = Path(path, size_threshold_mb,
                                            self.N_FILES_AT_A_TIME)
                path_handler = self.paths[path]
                if path_handler._size_threshold_mb != size_threshold_mb:
                    path_handler.finish()
                    path_handler = self.paths[path] = Path(
                        path, size_threshold_mb, self.N_FILES_AT_A_TIME)
                self._paths_accessed[path] = win32timezone.utcnow()
                files = sorted(path_handler.updated(),
                               key=operator.attrgetter("size"),
                               reverse=True)
                status = path_handler.status()

                #
                # If any path hasn't been queried for at least
                # three minutes, close the thread down and delete
                # its entry. If it is queried again, it will just
                # be restarted as new.
                #
                for path, last_accessed in self._paths_accessed.iteritems():
                    if (win32timezone.utcnow() - last_accessed).seconds > 180:
                        path_handler = self.paths.get(path)
                        if path_handler:
                            path_handler.finish()
                            del self.paths[path]
                            del self._paths_accessed[path]

        else:
            files = []
        return self.doc(files, status, form)
예제 #7
0
 def msg_meta(queue, message):
     """
     Add scalarizr version to meta
     """
     message.meta.update({
         'szr_version':
         __version__,
         'timestamp':
         os_time.utcnow().strftime("%a %d %b %Y %H:%M:%S %z")
     })
예제 #8
0
 def on_before_message_send(self, queue, message):
     """
     Add scalarizr version to meta
     """
     message.meta.update({
         'szr_version':
         scalarizr.__version__,
         'timestamp':
         os_time.utcnow().strftime("%a %d %b %Y %H:%M:%S %z")
     })
예제 #9
0
  def handler (self, form):
    path = form.get ("path", self.PATH)
    size_threshold_mb = int (form.get ("size_threshold_mb", self.SIZE_THRESHOLD_MB) or 0)
    refresh_secs = int (form.get ("refresh_secs", self.REFRESH_SECS) or 0)
    status = "Waiting"
    if path and fs.Dir (path):
      #
      # Ignore any non-existent paths, including garbage.
      # Create a new path handler if needed, or pull back
      # and existing one, and return the latest list.
      #
      with self._paths_lock:
        if path not in self.paths:
          self.paths[path] = Path (path, size_threshold_mb, self.N_FILES_AT_A_TIME)
        path_handler = self.paths[path]
        if path_handler._size_threshold_mb != size_threshold_mb:
          path_handler.finish ()
          path_handler = self.paths[path] = Path (path, size_threshold_mb, self.N_FILES_AT_A_TIME)
        self._paths_accessed[path] = win32timezone.utcnow ()
        files = sorted (path_handler.updated (), key=operator.attrgetter ("size"), reverse=True)
        status = path_handler.status ()

        #
        # If any path hasn't been queried for at least
        # three minutes, close the thread down and delete
        # its entry. If it is queried again, it will just
        # be restarted as new.
        #
        for path, last_accessed in self._paths_accessed.iteritems ():
          if (win32timezone.utcnow () - last_accessed).seconds > 180:
            path_handler = self.paths.get (path)
            if path_handler:
              path_handler.finish ()
              del self.paths[path]
              del self._paths_accessed[path]

    else:
      files = []
    return self.doc (files, status, form)
예제 #10
0
    def doc(self, files, status, form):
        path = form.get("path", self.PATH)
        top_n_files = int(form.get("top_n_files", self.TOP_N_FILES) or 0)
        size_threshold_mb = int(
            form.get("size_threshold_mb", self.SIZE_THRESHOLD_MB) or 0)
        refresh_secs = int(form.get("refresh_secs", self.REFRESH_SECS) or 0)
        highlight_days = int(
            form.get("highlight_days", self.HIGHLIGHT_DAYS) or 0)
        highlight_hrs = int(form.get("highlight_hrs", self.HIGHLIGHT_HRS) or 0)
        highlight_mins = int(
            form.get("highlight_mins", self.HIGHLIGHT_MINS) or 0)
        highlight_delta = datetime.timedelta(days=highlight_days,
                                             hours=highlight_hrs,
                                             minutes=highlight_mins)
        highlight_deltastamp = deltastamp(highlight_delta)
        if files:
            title = cgi.escape("Top %d files on %s over %dMb - %s" %
                               (min(len(files), self.TOP_N_FILES), path,
                                size_threshold_mb, status))
        else:
            title = cgi.escape("Top files on %s over %dMb - %s" %
                               (path, size_threshold_mb, status))

        doc = []
        doc.append("<html><head><title>%s</title>" % title)
        doc.append("""<style>
        body {font-family : calibri, verdana, sans-serif;}
        h1 {font-size : 120%;}
        form#params {font-size : 120%;}
        form#params input {font-family : calibri, verdana, sans-serif;}
        form#params span.label {font-weight : bold;}
        p.updated {margin-bottom : 1em; font-style : italic;}
        table {width : 100%;}
        thead tr {background-color : black; color : white; font-weight : bold;}
        table tr.odd {background-color : #ddd;}
        table tr.highlight td {background-color : #ffff80;}
        table td {padding-right : 0.5em;}
        table td.filename {width : 72%;}
        </style>""")
        doc.append("""<style media="print">
        form#params {display : none;}
        </style>""")
        doc.append("</head><body>")
        doc.append("""<form id="params" action="/" method="GET">
        <span class="label">Scan</span>&nbsp;<input type="text" name="path" value="%(path)s" size="20" maxlength="20" />&nbsp;
        <span class="label">for files over</span>&nbsp;<input type="text" name="size_threshold_mb" value="%(size_threshold_mb)s" size="5" maxlength="5" />Mb
        <span class="label">showing the top</span>&nbsp;<input type="text" name="top_n_files" value="%(top_n_files)s" size="3" maxlength="3" /> files
        <span class="label">refreshing every</span>&nbsp;<input type="text" name="refresh_secs" value="%(refresh_secs)s" size="3" maxlength="3" /> secs
        <span class="label">highlighting the last&nbsp;</span>&nbsp;<input type="text" name="highlight_days" value="%(highlight_days)s" size="3" maxlength="3" /> days
            </span>&nbsp;<input type="text" name="highlight_hrs" value="%(highlight_hrs)s" size="3" maxlength="3" /> hrs
            </span>&nbsp;<input type="text" name="highlight_mins" value="%(highlight_mins)s" size="3" maxlength="3" /> mins
        <input type="submit" value="Refresh" />
        </form><hr>""" % locals())

        now = win32timezone.utcnow()
        if path:
            doc.append("<h1>%s</h1>" % title)
            latest_filename = "\\".join(
                files[-1].parts[1:]) if files else "(no file yet)"
            doc.append(u'<p class="updated">Last updated %s</p>' %
                       time.asctime())
            doc.append(
                u'<table><thead><tr><td class="filename">Filename</td><td class="size">Size (Mb)</td><td class="updated">Updated</td></tr></thead>'
            )
            for i, f in enumerate(files[:top_n_files]):
                try:
                    doc.append(
                        u'<tr class="%s %s"><td class="filename">%s</td><td class="size">%5.2f</td><td class="updated">%s</td>'
                        %
                        ("odd" if i % 2 else "even", "highlight" if
                         ((now - max(f.written_at, f.created_at)) <=
                          highlight_delta) else "", f.relative_to(path).lstrip(
                              fs.seps), f.size / 1024.0 / 1024.0,
                         max(f.written_at, f.created_at)))
                except fs.exc.x_winsys:
                    pass
            doc.append("</table>")

        doc.append("</body></html>")
        return doc
예제 #11
0
  def doc (self, files, status, form):
    path = form.get ("path", self.PATH)
    top_n_files = int (form.get ("top_n_files", self.TOP_N_FILES) or 0)
    size_threshold_mb = int (form.get ("size_threshold_mb", self.SIZE_THRESHOLD_MB) or 0)
    refresh_secs = int (form.get ("refresh_secs", self.REFRESH_SECS) or 0)
    highlight_days = int (form.get ("highlight_days", self.HIGHLIGHT_DAYS) or 0)
    highlight_hrs = int (form.get ("highlight_hrs", self.HIGHLIGHT_HRS) or 0)
    highlight_mins = int (form.get ("highlight_mins", self.HIGHLIGHT_MINS) or 0)
    highlight_delta = datetime.timedelta (days=highlight_days, hours=highlight_hrs, minutes=highlight_mins)
    highlight_deltastamp = deltastamp (highlight_delta)
    if files:
      title = cgi.escape ("Top %d files on %s over %dMb - %s" % (min (len (files), self.TOP_N_FILES), path, size_threshold_mb, status))
    else:
      title = cgi.escape ("Top files on %s over %dMb - %s" % (path, size_threshold_mb, status))

    doc = []
    doc.append (u"<html><head><title>%s</title>" % title)
    doc.append (u"""<style>
    body {font-family : calibri, verdana, sans-serif;}
    h1 {font-size : 120%;}
    form#params {font-size : 120%;}
    form#params input {font-family : calibri, verdana, sans-serif;}
    form#params span.label {font-weight : bold;}
    p.updated {margin-bottom : 1em; font-style : italic;}
    table {width : 100%;}
    thead tr {background-color : black; color : white; font-weight : bold;}
    table tr.odd {background-color : #ddd;}
    table tr.highlight td {background-color : #ffff80;}
    table td {padding-right : 0.5em;}
    table td.filename {width : 72%;}
    </style>""")
    doc.append (u"""<style media="print">
    form#params {display : none;}
    </style>""")
    doc.append (u"</head><body>")
    doc.append (u"""<form id="params" action="/" method="GET">
    <span class="label">Scan</span>&nbsp;<input type="text" name="path" value="%(path)s" size="20" maxlength="20" />&nbsp;
    <span class="label">for files over</span>&nbsp;<input type="text" name="size_threshold_mb" value="%(size_threshold_mb)s" size="5" maxlength="5" />Mb
    <span class="label">showing the top</span>&nbsp;<input type="text" name="top_n_files" value="%(top_n_files)s" size="3" maxlength="3" /> files
    <span class="label">refreshing every</span>&nbsp;<input type="text" name="refresh_secs" value="%(refresh_secs)s" size="3" maxlength="3" /> secs
    <span class="label">highlighting the last&nbsp;</span>&nbsp;<input type="text" name="highlight_days" value="%(highlight_days)s" size="3" maxlength="3" /> days
      </span>&nbsp;<input type="text" name="highlight_hrs" value="%(highlight_hrs)s" size="3" maxlength="3" /> hrs
      </span>&nbsp;<input type="text" name="highlight_mins" value="%(highlight_mins)s" size="3" maxlength="3" /> mins
    <input type="submit" value="Refresh" />
    </form><hr>""" % locals ())

    now = win32timezone.utcnow ()
    if path:
      doc.append (u"<h1>%s</h1>" % title)
      latest_filename = "\\".join (files[-1].parts[1:]) if files else "(no file yet)"
      doc.append (u'<p class="updated">Last updated %s</p>' % time.asctime ())
      doc.append (u'<table><thead><tr><td class="filename">Filename</td><td class="size">Size (Mb)</td><td class="updated">Updated</td></tr></thead>')
      for i, f in enumerate (files[:top_n_files]):
        try:
          doc.append (
            u'<tr class="%s %s"><td class="filename">%s</td><td class="size">%5.2f</td><td class="updated">%s</td>' % (
              "odd" if i % 2 else "even",
              "highlight" if ((now - max (f.written_at, f.created_at)) <= highlight_delta) else "",
              f.relative_to (path).lstrip (fs.seps),
              f.size / 1024.0 / 1024.0,
              max (f.written_at, f.created_at)
            )
          )
        except fs.exc.x_winsys:
          pass
      doc.append (u"</table>")

    doc.append (u"</body></html>")
    return doc