[lua-torch-sys] 01/01: Imported Upstream version 0~20160415-g8d2b8fa

Zhou Mo cdluminate-guest at moszumanska.debian.org
Fri Jun 17 09:41:30 UTC 2016


This is an automated email from the git hooks/post-receive script.

cdluminate-guest pushed a commit to branch master
in repository lua-torch-sys.

commit 6421d6bc689063dad9b8d4b0b4cd89f3cdbc76c2
Author: Zhou Mo <cdluminate at gmail.com>
Date:   Fri Jun 17 09:38:23 2016 +0000

    Imported Upstream version 0~20160415-g8d2b8fa
---
 .gitignore         |   1 +
 CMakeLists.txt     |  14 +++++
 LICENSE            |  32 ++++++++++++
 README.md          |  78 ++++++++++++++++++++++++++++
 colors.lua         |  32 ++++++++++++
 fpath.lua          |  10 ++++
 init.lua           | 149 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 sys-1.0-0.rockspec |  26 ++++++++++
 sys-1.1-0.rockspec |  25 +++++++++
 sys.c              |  81 +++++++++++++++++++++++++++++
 10 files changed, 448 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..567609b
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+build/
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..baa0309
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,14 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6 FATAL_ERROR)
+CMAKE_POLICY(VERSION 2.6)
+
+FIND_PACKAGE(Torch REQUIRED)
+
+SET(src sys.c)
+SET(luasrc init.lua colors.lua fpath.lua)
+
+ADD_TORCH_PACKAGE(sys "${src}" "${luasrc}")
+
+TARGET_LINK_LIBRARIES(sys luaT TH)
+IF(LUALIB)
+  TARGET_LINK_LIBRARIES(sys ${LUALIB})
+ENDIF()
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..ff79482
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,32 @@
+Copyright (c) 2011-2014 Idiap Research Institute (Ronan Collobert)
+Copyright (c) 2011-2012 NEC Laboratories America (Koray Kavukcuoglu)
+Copyright (c) 2011-2013 NYU (Clement Farabet)
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in the
+   documentation and/or other materials provided with the distribution.
+
+3. Neither the names of NEC Laboratories American and IDIAP Research
+   Institute nor the names of its contributors may be used to endorse or
+   promote products derived from this software without specific prior
+   written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..0aabb3b
--- /dev/null
+++ b/README.md
@@ -0,0 +1,78 @@
+# Lua *system* package
+
+Note: some functions only work on UNIX systems.
+
+## Dependencies
+Torch7 (www.torch.ch)
+
+## Install
+```
+$ luarocks install sys
+```
+
+## Use
+
+```lua
+$ torch
+> require 'sys'
+```
+
+### Time / Clock
+
+```lua
+> t = sys.clock()  -- high precision clock (us precision)
+> sys.tic()
+> -- do something
+> t = sys.toc()    -- high precision tic/toc
+> sys.sleep(1.5)   -- sleep 1.5 seconds
+```
+
+### Paths
+
+```lua
+> path,fname = sys.fpath()
+```
+
+Always returns the path of the file in which this call is made. Useful
+to access local resources (non-lua files).
+
+### Execute
+
+By default, Lua's `os.execute` doesn't pipe its results (stdout). This
+function uses popen to pipe its results into a Lua string:
+
+```lua
+> res = sys.execute('ls -l')
+> print(res)
+```
+
+Derived from this, a few commands:
+
+```lua
+> print(sys.uname())
+linux
+```
+
+UNIX-only: shortcuts to run bash commands:
+
+```lua
+> ls()
+> ll()
+> lla()
+```
+
+### sys.COLORS
+
+If you'd like print in colours, follow the following snippets of code. Let start by listing the available colours
+
+```lua
+$ torch
+> for k in pairs(sys.COLORS) do print(k) end
+```
+
+Then, we can generate a shortcut `c = sys.COLORS` and use it within a `print`
+
+```lua
+> c = sys.COLORS
+> print(c.magenta .. 'This ' .. c.red .. 'is ' .. c.yellow .. 'a ' .. c.green .. 'rainbow' .. c.cyan .. '!')
+```
diff --git a/colors.lua b/colors.lua
new file mode 100644
index 0000000..f6543e5
--- /dev/null
+++ b/colors.lua
@@ -0,0 +1,32 @@
+--------------------------------------------------------------------------------
+-- colors, can be used to print things in color
+--------------------------------------------------------------------------------
+local colors = {
+   none = '\27[0m',
+   black = '\27[0;30m',
+   red = '\27[0;31m',
+   green = '\27[0;32m',
+   yellow = '\27[0;33m',
+   blue = '\27[0;34m',
+   magenta = '\27[0;35m',
+   cyan = '\27[0;36m',
+   white = '\27[0;37m',
+   Black = '\27[1;30m',
+   Red = '\27[1;31m',
+   Green = '\27[1;32m',
+   Yellow = '\27[1;33m',
+   Blue = '\27[1;34m',
+   Magenta = '\27[1;35m',
+   Cyan = '\27[1;36m',
+   White = '\27[1;37m',
+   _black = '\27[40m',
+   _red = '\27[41m',
+   _green = '\27[42m',
+   _yellow = '\27[43m',
+   _blue = '\27[44m',
+   _magenta = '\27[45m',
+   _cyan = '\27[46m',
+   _white = '\27[47m'
+}
+
+return colors
diff --git a/fpath.lua b/fpath.lua
new file mode 100644
index 0000000..9e4854c
--- /dev/null
+++ b/fpath.lua
@@ -0,0 +1,10 @@
+--------------------------------------------------------------------------------
+-- always returns the path of the file running
+--------------------------------------------------------------------------------
+local function fpath()
+   local fpath = _G.debug.getinfo(2).source:gsub('@','')
+   if fpath:find('/') ~= 1 then fpath = paths.concat(paths.cwd(),fpath) end
+   return paths.dirname(fpath),paths.basename(fpath)
+end
+
+return fpath
diff --git a/init.lua b/init.lua
new file mode 100644
index 0000000..4596a92
--- /dev/null
+++ b/init.lua
@@ -0,0 +1,149 @@
+----------------------------------------------------------------------
+-- sys - a package that provides simple system (unix) tools
+----------------------------------------------------------------------
+
+local os = require 'os'
+local io = require 'io'
+local paths = require 'paths'
+
+sys = {}
+
+--------------------------------------------------------------------------------
+-- load all functions from lib
+--------------------------------------------------------------------------------
+local _lib = require 'libsys'
+for k,v in pairs(_lib) do
+   sys[k] = v
+end
+
+--------------------------------------------------------------------------------
+-- tic/toc (matlab-like) timers
+--------------------------------------------------------------------------------
+local __t__
+function sys.tic()
+   __t__ = sys.clock()
+end
+function sys.toc(verbose)
+   local __dt__ = sys.clock() - __t__
+   if verbose then print(__dt__) end
+   return __dt__
+end
+
+--------------------------------------------------------------------------------
+-- execute an OS command, but retrieves the result in a string
+--------------------------------------------------------------------------------
+local function execute(cmd)
+   local cmd = cmd .. ' 2>&1'
+   local f = io.popen(cmd)
+   local s = f:read('*all')
+   f:close()
+   s = s:gsub('^%s*',''):gsub('%s*$','')
+   return s
+end
+sys.execute = execute
+
+--------------------------------------------------------------------------------
+-- execute an OS command, but retrieves the result in a string
+-- side effect: file in /tmp
+-- this call is typically more robust than the one above (on some systems)
+--------------------------------------------------------------------------------
+function sys.fexecute(cmd, readwhat)
+   local tmpfile = os.tmpname()
+   local cmd = cmd .. ' 1>'.. tmpfile..' 2>' .. tmpfile
+   os.execute(cmd)
+   local file = _G.assert(io.open(tmpfile))
+   local s = file:read('*all')
+   file:close()
+   s = s:gsub('^%s*',''):gsub('%s*$','')
+   os.execute('rm ' .. tmpfile)
+   return s
+end
+
+--------------------------------------------------------------------------------
+-- returns the name of the OS in use
+-- warning, this method is extremely dumb, and should be replaced by something
+-- more reliable
+--------------------------------------------------------------------------------
+function sys.uname()
+   if paths.dirp('C:\\') then
+      return 'windows'
+   else
+      local os = execute('uname -a')
+      if os:find('Linux') then
+         return 'linux'
+      elseif os:find('Darwin') then
+         return 'macos'
+      else
+         return '?'
+      end
+   end
+end
+sys.OS = sys.uname()
+
+--------------------------------------------------------------------------------
+-- ls (list dir)
+--------------------------------------------------------------------------------
+sys.ls  = function(d) d = d or ' ' return execute('ls '    ..d) end
+sys.ll  = function(d) d = d or ' ' return execute('ls -l ' ..d) end
+sys.la  = function(d) d = d or ' ' return execute('ls -a ' ..d) end
+sys.lla = function(d) d = d or ' ' return execute('ls -la '..d) end
+
+--------------------------------------------------------------------------------
+-- prefix
+--------------------------------------------------------------------------------
+sys.prefix = execute('which lua'):gsub('//','/'):gsub('/bin/lua\n','')
+
+--------------------------------------------------------------------------------
+-- always returns the path of the file running
+--------------------------------------------------------------------------------
+sys.fpath = require 'sys.fpath'
+
+--------------------------------------------------------------------------------
+-- split string based on pattern pat
+--------------------------------------------------------------------------------
+function sys.split(str, pat)
+   local t = {}  -- NOTE: use {n = 0} in Lua-5.0
+   local fpat = "(.-)" .. pat
+   local last_end = 1
+   local s, e, cap = str:find(fpat, last_end)
+   while s do
+      if s ~= 1 or cap ~= "" then
+         _G.table.insert(t,cap)
+      end
+      last_end = e+1
+      s, e, cap = str:find(fpat, last_end)
+   end
+   if last_end <= #str then
+      cap = str:sub(last_end)
+      _G.table.insert(t, cap)
+   end
+   return t
+end
+
+--------------------------------------------------------------------------------
+-- check if a number is NaN
+--------------------------------------------------------------------------------
+function sys.isNaN(number)
+   -- We rely on the property that NaN is the only value that doesn't equal itself.
+   return (number ~= number)
+end
+
+--------------------------------------------------------------------------------
+-- sleep
+--------------------------------------------------------------------------------
+function sys.sleep(seconds)
+   sys.usleep(seconds*1000000)
+end
+
+--------------------------------------------------------------------------------
+-- colors, can be used to print things in color
+--------------------------------------------------------------------------------
+sys.COLORS = require 'sys.colors'
+
+--------------------------------------------------------------------------------
+-- backward compat
+--------------------------------------------------------------------------------
+sys.dirname = paths.dirname
+sys.concat = paths.concat
+
+return sys
diff --git a/sys-1.0-0.rockspec b/sys-1.0-0.rockspec
new file mode 100644
index 0000000..bc34425
--- /dev/null
+++ b/sys-1.0-0.rockspec
@@ -0,0 +1,26 @@
+package = "sys"
+version = "1.0-0"
+
+source = {
+   url = "git://github.com/torch/sys",
+   tag = "1.0-0"
+}
+
+description = {
+   summary = "A system library for Torch",
+   detailed = [[
+Provides system functionalities for Torch.
+   ]],
+   homepage = "https://github.com/torch/sys",
+   license = "BSD"
+}
+
+dependencies = {
+   "torch >= 7.0",
+}
+
+build = {
+   type = "command",
+   build_command = [[cmake -E make_directory build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="$(LUA_BINDIR)/.." -DCMAKE_INSTALL_PREFIX="$(PREFIX)" && $(MAKE)]],
+   install_command = "cd build && $(MAKE) install"
+}
diff --git a/sys-1.1-0.rockspec b/sys-1.1-0.rockspec
new file mode 100644
index 0000000..c362988
--- /dev/null
+++ b/sys-1.1-0.rockspec
@@ -0,0 +1,25 @@
+package = "sys"
+version = "1.1-0"
+
+source = {
+   url = "git://github.com/torch/sys"
+}
+
+description = {
+   summary = "A system library for Torch",
+   detailed = [[
+Provides system functionalities for Torch.
+   ]],
+   homepage = "https://github.com/torch/sys",
+   license = "BSD"
+}
+
+dependencies = {
+   "torch >= 7.0",
+}
+
+build = {
+   type = "command",
+   build_command = [[cmake -E make_directory build && cd build && cmake .. -DLUALIB=$(LUALIB) -DCMAKE_BUILD_TYPE=Release  -DCMAKE_INSTALL_PREFIX="$(PREFIX)" && $(MAKE)]],
+   install_command = "cd build && $(MAKE) install"
+}
diff --git a/sys.c b/sys.c
new file mode 100644
index 0000000..5425723
--- /dev/null
+++ b/sys.c
@@ -0,0 +1,81 @@
+#include <lua.h>
+#include <lauxlib.h>
+
+#ifdef _WIN32
+
+#define WINDOWS_LEAN_AND_MEAN
+#include <windows.h>
+#include <stdint.h>
+
+static int l_clock(lua_State *L) {
+    static const uint64_t EPOCH = 116444736000000000ULL;
+    SYSTEMTIME  systemtime;
+    FILETIME filetime;
+    uint64_t time;
+    GetSystemTime(&systemtime);
+    GetSystemTimeAsFileTime(&filetime);
+    time = (((uint64_t)filetime.dwHighDateTime) << 32) + ((uint64_t)filetime.dwLowDateTime);
+    double precise_time = (time - EPOCH) / 10000000.0;
+    lua_pushnumber(L, precise_time);
+    return 1;
+}
+
+static int l_usleep(lua_State *L) {
+  int time = 1;
+  if (lua_isnumber(L, 1)) time = lua_tonumber(L, 1);
+  Sleep(time / 1000);
+  return 1;
+}
+
+#else
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+
+#include <sys/time.h>
+#include <unistd.h>
+#include <time.h>
+
+static int l_clock(lua_State *L) {
+  struct timeval tv;
+  struct timezone tz;
+  struct tm *tm;
+  gettimeofday(&tv, &tz);
+  tm=localtime(&tv.tv_sec);
+  double precise_time = tv.tv_sec + tv.tv_usec / 1e6;
+  lua_pushnumber(L,precise_time);
+  return 1;
+}
+
+static int l_usleep(lua_State *L) {
+  int time = 1;
+  if (lua_isnumber(L, 1)) time = lua_tonumber(L, 1);
+  usleep(time);
+  return 1;
+}
+
+#endif
+
+static const struct luaL_Reg routines [] = {
+  {"clock", l_clock},
+  {"usleep", l_usleep},
+  {NULL, NULL}
+};
+
+#if defined(_WIN32)
+    #define SYS_DLLEXPORT __declspec(dllexport) __cdecl
+#else
+    #define SYS_DLLEXPORT 
+#endif
+int SYS_DLLEXPORT luaopen_libsys(lua_State *L)
+{
+  lua_newtable(L);
+#if LUA_VERSION_NUM == 501
+  luaL_register(L, NULL, routines);
+#else
+  luaL_setfuncs(L, routines, 0);
+#endif
+  return 1;
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/lua-torch-sys.git



More information about the debian-science-commits mailing list