Commit f8d310c9 authored by Leo Iannacone's avatar Leo Iannacone

better code style on utils

parent 70646bc3
__errors_handler = (from, err, socket) -> path = require("path")
from = "NO SOCKET: " + from unless socket fs = require("fs")
console.error from, err.message config = require("./config")
socket.emit config.events.error, err.message if socket Tail = require("./tail")
return
__check_no_backward = (backward_path) -> _check_no_backward = (backward_path) ->
try try
return backward_path.indexOf("..") < 0 return backward_path.indexOf("..") < 0
catch err catch err
return true return true
return return
__check_data_distribution = (data) -> check_data_distribution = (data) ->
__check_no_backward(data) and _check_no_backward(data) and
__check_no_backward(data.distribution) and _check_no_backward(data.distribution) and
__check_no_backward(data.distribution.name) _check_no_backward(data.distribution.name)
check_data_package = (data) ->
check_data_distribution(data) and
_check_no_backward(data.package) and
_check_no_backward(data.package.name) and
_check_no_backward(data.package.version)
__check_data_package = (data) ->
__check_data_distribution(data) and
__check_no_backward(data.package) and
__check_no_backward(data.package.name) and
__check_no_backward(data.package.version)
__check_data_file = (data) -> check_data_file = (data) ->
__check_data_package(data) and check_data_package(data) and
__check_no_backward(data.file) and _check_no_backward(data.file) and
__check_no_backward(data.file.name) _check_no_backward(data.file.name)
__get_distribution_pool_path = (data) ->
get_distribution_pool_path = (data) ->
path.join(config.debomatic.path, data.distribution.name, "pool") path.join(config.debomatic.path, data.distribution.name, "pool")
__get_package_path = (data) ->
path.join(__get_distribution_pool_path(data), data.package.orig_name)
__get_file_path = (data) -> get_package_path = (data) ->
path.join(__get_package_path(data), path.join(get_distribution_pool_path(data), data.package.orig_name)
get_file_path = (data) ->
path.join(get_package_path(data),
data.package.orig_name + "." + data.file.name) data.package.orig_name + "." + data.file.name)
__get_files_list = (dir, onlyDirectories, callback) -> get_files_list = (dir, onlyDirectories, callback) ->
fs.readdir dir, (err, files) -> fs.readdir dir, (err, files) ->
result = []
if err if err
__errors_handler "__get_files_list", err __errors_handler "__get_files_list", err
return return
files.forEach (f) -> result = []
for f in files
try try
complete_path = path.join(dir, f) complete_path = path.join(dir, f)
stat = fs.statSync(complete_path) stat = fs.statSync(complete_path)
if onlyDirectories if onlyDirectories
result.push f if stat.isDirectory() result.push(f) if stat.isDirectory()
else else
result.push f if stat.isFile() result.push(f) if stat.isFile()
catch fs_error catch fs_error
__errors_handler "__get_files_list:forEach", fs_error __errors_handler("__get_files_list:forEach", fs_error)
return continue
return callback(result)
callback result
return
return watch_path_onsocket = (event_name, socket, data, watch_path, updater) ->
__watch_path_onsocket = (event_name, socket, data, watch_path, updater) ->
socket_watchers = socket.watchers or {} socket_watchers = socket.watchers or {}
try try
watcher = socket_watchers[event_name] watcher = socket_watchers[event_name]
watcher.close() if watcher watcher.close() if watcher
fs.stat watch_path, (err, stats) -> fs.stat watch_path, (err, stats) ->
if err if err
__errors_handler "__watch_path_onsocket:fs.stat", err, socket __errors_handler("__watch_path_onsocket:fs.stat",
err, socket)
return return
if stats.isDirectory() if stats.isDirectory()
watcher = fs.watch(watch_path, watcher = fs.watch(watch_path,
persistent: true persistent: true,
, (event, fileName) -> (event, fileName) ->
updater event_name, socket, data if event is "rename" if ev is "rename"
return updater(event_name, socket, data))
)
else if stats.isFile() else if stats.isFile()
watcher = new Tail(watch_path) watcher = new Tail(watch_path)
watcher.on "line", (new_content, tailInfo) -> watcher.on("line", (new_content, tailInfo) ->
data.file.new_content = new_content + "\n" data.file.new_content = new_content + "\n"
updater event_name, socket, data updater event_name, socket, data)
return
watcher.on "error", (msg) -> watcher.on "error", (msg) ->
socket.emit config.events.error, msg socket.emit config.events.error, msg
return
socket_watchers[event_name] = watcher socket_watchers[event_name] = watcher
socket.watchers = socket_watchers socket.watchers = socket_watchers
return
catch err catch err
__errors_handler("__watch_path_onsocket <- " + errors_handler("__watch_path_onsocket <- " +
arguments_.callee.caller.name, arguments_.callee.caller.name,
err, err, socket)
socket)
return
return return
__generic_handler_watcher = (event_name, socket, data, watch_path, callback) ->
generic_handler_watcher = (event_name, socket, data, watch_path, callback) ->
callback event_name, socket, data callback event_name, socket, data
__watch_path_onsocket event_name, socket, data, watch_path, callback watch_path_onsocket event_name, socket, data, watch_path, callback
return
__send_distributions = (socket) -> send_distributions = (socket) ->
__get_files_list config.debomatic.path, true, (directories) -> get_files_list config.debomatic.path, true, (directories) ->
distributions = [] distributions = []
directories.forEach (dir) -> for dir in directories
data = {} data = {}
data.distribution = {} data.distribution = {}
data.distribution.name = dir data.distribution.name = dir
pool_path = __get_distribution_pool_path(data) pool_path = get_distribution_pool_path(data)
distributions.push dir if fs.existsSync(pool_path) distributions.push dir if fs.existsSync(pool_path)
return
socket.emit config.events.broadcast.distributions, distributions socket.emit config.events.broadcast.distributions, distributions
return
return
path = require("path")
fs = require("fs")
config = require("./config")
Tail = require("./tail")
utils =
check_data_distribution: (data) ->
__check_data_distribution data
check_data_package: (data) -> errors_handler = (from, error, socket) ->
__check_data_package data from = "NO SOCKET: " + from unless socket
console.error from, err.message
check_data_file: (data) -> socket.emit config.events.error, err.message if socket
__check_data_file data return
get_distribution_pool_path: (data) ->
__get_distribution_pool_path data
get_package_path: (data) ->
__get_package_path data
get_file_path: (data) ->
__get_file_path data
get_files_list: (dir, onlyDirectories, callback) ->
__get_files_list dir, onlyDirectories, callback
watch_path_onsocket: (event_name, socket, data, watch_path, updater) ->
__watch_path_onsocket event_name, socket, data, watch_path, updater
generic_handler_watcher: (event_name, socket, data, watch_path, callback) ->
__generic_handler_watcher event_name, socket, data, watch_path, callback
send_distributions: (socket) ->
__send_distributions socket
errors_handler: (from, error, socket) ->
__errors_handler from, error, socket
module.exports = utils module.exports.check_data_distribution = check_data_distribution
module.exports.check_data_package = check_data_package
module.exports.check_data_file = check_data_file
module.exports.get_distribution_pool_path = get_distribution_pool_path
module.exports.get_package_path = get_package_path
module.exports.get_file_path = get_file_path
module.exports.get_files_list = get_files_list
module.exports.watch_path_onsocket = watch_path_onsocket
module.exports.generic_handler_watcher = generic_handler_watcher
module.exports.send_distributions = send_distributions
module.exports.errors_handler = errors_handler
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment