diff mbox

[OpenWrt-Devel,V2] scripts: python based server for opkg packages and .bin

Message ID 1428588898-13571-1-git-send-email-javier.domingo@fon.com
State Rejected
Headers show

Commit Message

Javier Domingo Cansino April 9, 2015, 2:14 p.m. UTC
This patch adds a small script that will expose the built packages and the
images to ease opkg and sysupgrade use during development. Compatible with
Python2 and Python3.

---
 scripts/package-server.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
 create mode 100755 scripts/package-server.py

Comments

Karl Palsson April 10, 2015, 4:32 p.m. UTC | #1
Javier Domingo Cansino <javierdo1@gmail.com> wrote:
> This patch adds a small script that will expose the built packages and
> the
> images to ease opkg and sysupgrade use during development. Compatible
> with
> Python2 and Python3.
> 

What does this do that isn't done by "python -m SimpleHTTPServer"
already?
Javier Domingo Cansino April 10, 2015, 5:53 p.m. UTC | #2
​It goes to bin/ and looks for the latest updated bin/XXXX directory.

Other ideas on top of this if you see it interesting:
 - Add inotify to be aware on when other folders at bin/ get updated and
change the working directory for the server automatically.
 - Make a 302 redirect for sysupgrade.bin to point to the latest .bin
generated (configurable), to avoid having to do the symlink myself in bin/
(to ease wget for sysupgrade.bin)

At the end I wanted to keep it the simpler, but I open to more suggestions
diff mbox

Patch

diff --git a/scripts/package-server.py b/scripts/package-server.py
new file mode 100755
index 0000000..3954781
--- /dev/null
+++ b/scripts/package-server.py
@@ -0,0 +1,50 @@ 
+#!/usr/bin/env python
+from __future__ import print_function
+import operator
+import os
+import sys
+
+try:
+    from SimpleHTTPServer import SimpleHTTPRequestHandler as Handler
+except ImportError:
+    from http.server import SimpleHTTPRequestHandler as Handler
+try:
+    from SocketServer import TCPServer
+except ImportError:
+    from socketserver import TCPServer
+
+PORT = 80
+
+
+def find_last_modification_date(path):
+    time = 0
+    for root, dirs, files in os.walk(path):
+        for path in files:
+            _, _, _, _, _, _, _, _, last_m, _ = os.stat(
+                os.path.join(root, path))
+            time = time if time > last_m else last_m
+    return time
+
+
+(dirpath, dirnames, _) = next(os.walk(os.path.join(os.path.dirname(__file__),
+                                                   '../bin')))
+
+m_times = {}
+
+for dir in dirnames:
+    path = os.path.join(dirpath, dir)
+    m_times[path] = find_last_modification_date(path)
+dir, m_path = sorted(m_times.items(), key=operator.itemgetter(1),
+                     reverse=True)[0]
+dir = os.path.relpath(dir)
+print('Selecting', dir, 'as working dir for', m_path, 'modification date.')
+os.chdir(dir)
+
+try:
+    httpd = TCPServer(("", PORT), Handler)
+except PermissionError:
+
+    sys.stderr.write('Permission denied for listening in port %d\n' % PORT)
+    sys.exit(1)
+print("HTTP server started at port", PORT)
+httpd.serve_forever()